Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

I want you to modify the simulate projectile function: Start the parabolic movement at…

  1. User

    I want you to modify the simulate projectile function:

    *Start the parabolic movement at the initial transform. position *The maximum height is reached at the middle of the trajectory *End the movement in target, take into account target will move so the end destination is dynamic *the longer the distance the arrow goes up, the shorter the distance the arrow goes down, the minimum height is a straight line with the target

    using System.Collections; using Game.Systems; using UnityEngine;

    namespace Game.ImprovedAI { public class Arrow : MonoBehaviour { public float speed = 10f; public float maxHeightFactor = 0.5f; // Adjust the height of the projectile public float damageRange; public float chaseRange; public GameObject target;

        private Vector3 startPoint;
        private Vector3 currentTarget;
        private float travelTime;
        private UnitHealth unitHealth;
        private float damage;
    
        public void Init(GameObject target, UnitHealth unitHealth, float damage)
        {
            this.target = target;
            this.unitHealth = unitHealth;
            this.damage = damage;
            startPoint = transform.position;
            currentTarget = target.transform.position;
            InitiateFlight();
        }
    
        void InitiateFlight()
        {
            float distance = Vector3.Distance(startPoint, currentTarget);
            float maxHeight = Mathf.Max(distance * maxHeightFactor, 0.01f); // Ensure some height
            travelTime = distance / speed;
    
            StartCoroutine(SimulateProjectile(maxHeight));
        }
    
        IEnumerator SimulateProjectile(float maxHeight)
        {
            float time = 0;
    
            // Calculate the vertex of the parabola (the highest point)
            Vector3 vertex = (startPoint + currentTarget) / 2 + Vector3.up * maxHeight;
    
            bool updatedTarget = false;
    
            while (time < travelTime)
            {
                time += Time.deltaTime;
    
    
    
                // Calculate the lerp parameter in [0, 1]
                float lerpParameter = time / travelTime;
                float heightParameter = 4 * (-lerpParameter * lerpParameter + lerpParameter);
    
                Vector3 parabolaPoint = Vector3.Lerp(startPoint, currentTarget, lerpParameter) + Vector3.up * maxHeight * heightParameter;
    
                // Rotate the arrow to face the next position
                RotateTowardsMovement(parabolaPoint);
                // Move the arrow to the next calculated position
                transform.position = parabolaPoint;
    
                if (Vector3.Distance(transform.position, currentTarget) < damageRange)
                {
                    unitHealth?.TakeDamage(damage);
                    break;
                }
    
                yield return null;
            }
    
            OnReachTarget();
        }
    
    
        void RotateTowardsMovement(Vector3 nextPosition)
        {
            Vector3 directionOfMovement = nextPosition - transform.position;
            if (directionOfMovement != Vector3.zero)
            {
                Quaternion rotation = Quaternion.LookRotation(directionOfMovement.normalized);
                transform.rotation = rotation;
            }
        }
    
        void OnReachTarget()
        {
            Destroy(gameObject);
        }
    }
    

    }

  2. Assistant

    To adjust the Arrow class to meet the new requirements, we need to update the SimulateProjectile coroutine to account for a moving target and to modify the parabola calculations. Here's a modified version of the function, including the required adjustments to the Arrow class:

    using System.Collections;
    using UnityEngine;
    
    namespace Game.ImprovedAI
    {
        public class Arrow : MonoBehaviour
        {
            public float speed = 10f;
            public float minimumHeight = 0.5f;
            public float maxHeightFactor = 0.5f;
            public float damageRange;
            public float chaseRange;
            public GameObject target;
    
            private Vector3 startPoint;
            private float travelTime;
            private UnitHealth unitHealth;
            private float damage;
    
            public void Init(GameObject target, UnitHealth unitHealth, float damage)
            {
                this.target = target;
                this.unitHealth = unitHealth;
                this.damage = damage;
                startPoint = transform.position;
                InitiateFlight();
            }
    
            void InitiateFlight()
            {
                StartCoroutine(SimulateProjectile());
            }
    
            IEnumerator SimulateProjectile()
            {
                float time = 0;
                Vector3 currentTarget = target.transform.position;
    
                // Calculate initial dynamic max height based on start and target positions
                float initialDistance = Vector3.Distance(startPoint, currentTarget);
                float maxHeight = Mathf.Max(initialDistance * maxHeightFactor, minimumHeight);
    
                // Begin projectile movement simulation
                while (target != null)
                {
                    time += Time.deltaTime;
                    currentTarget = target.transform.position; // Update target position in case it's moving
    
                    // Calculate new max height based on remaining distance to the target
                    float remainingDistanceToTarget = Vector3.Distance(transform.position, currentTarget);
                    float dynamicMaxHeight = Mathf.Clamp(maxHeight * (remainingDistanceToTarget / initialDistance), minimumHeight, maxHeight);
    
                    // Calculate time ratio to determine where we are in the arc
                    float ascentTime = travelTime * 0.5f;
                    bool isAscending = time < ascentTime;
                    float heightTimeRatio = (isAscending ? time / ascentTime : (travelTime - time) / ascentTime);
    
                    // Calculate height based on parabolic arc
                    float height = 4 * dynamicMaxHeight * heightTimeRatio * (1 - heightTimeRatio);
    
                    // Calculate next position
                    float travelDistance = speed * time;
                    float totalDistance = Vector3.Distance(startPoint, currentTarget);
                    float lerpParameter = Mathf.Clamp(travelDistance / totalDistance, 0f, 1f);
                    Vector3 nextPosition = Vector3.Lerp(startPoint, currentTarget, lerpParameter) + Vector3.up * height;
    
                    // Face the arrow towards the next position and move the arrow
                    RotateTowardsMovement(nextPosition);
                    transform.position = nextPosition;
    
                    // Check if within damage range to the moving target
                    if (remainingDistanceToTarget < damageRange)
                    {
                        unitHealth?.TakeDamage(damage);
                        break;
                    }
    
                    yield return null;
    
                    // Recalculate travel time based on remaining distance and speed
                    travelTime = remainingDistanceToTarget / speed;
                }
    
                OnReachTarget();
            }
    
            void RotateTowardsMovement(Vector3 nextPosition)
            {
                Vector3 directionOfMovement = nextPosition - transform.position;
                if (directionOfMovement != Vector3.zero)
                {
                    Quaternion rotation = Quaternion.LookRotation(directionOfMovement.normalized);
                    transform.rotation = rotation;
                }
            }
    
            void OnReachTarget()
            {
                Destroy(gameObject);
            }
        }
    }
    

    Please note the following changes:

    1. The SimulateProjectile no longer receives the maxHeight parameter, as the function now calculates maxHeight dynamically based on the initial distance and the changing distance to the target.

    2. The function updates the target's position during each iteration to account for a moving target.

    3. The arc height is dynamically updated as the projectile gets closer to the target.

    4. The RotateTowardsMovement method and OnReachTarget method remain unchanged but are crucial for the arrow behavior.

    Make sure that the speed, damageRange, chaseRange, maximumHeightFactor, and minimumHeight are set appropriately for your game to function well. Adjust them as needed to achieve the desired projectile behavior.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.