Пример #1
0
    // Update is called once per frame
    void Update()
    {
        // TODO 7: Very similar to arrive, but using angular velocities
        // Find the desired rotation and accelerate to it
        // Use Vector3.SignedAngle() to find the angle between two direction

        Vector3 desired_v = (move.target.transform.position - move.transform.position);
        Vector3 axis      = new Vector3(0, 1, 0);
        float   angle     = Vector3.SignedAngle(move.movement, desired_v, axis);

        //float distance = (target - move.transform.position).magnitude;
        if (angle <= min_angle)
        {
            move.SetRotationVelocity(0.0f);
        }
        else
        {
            float acceleration = 0;
            if (angle < slow_angle)
            {
                acceleration = move.max_rot_velocity * angle / slow_angle;
            }

            acceleration /= time_to_target;

            if (angle < 0)
            {
                acceleration -= acceleration;
            }
            move.AccelerateRotation(angle);
        }
    }
Пример #2
0
    // Update is called once per frame
    void Update()
    {
        // TODO 4: As with arrive, we first construct our ideal rotation
        // then accelerate to it. Use Mathf.DeltaAngle() to wrap around PI
        // Is the same as arrive but with angular velocities
        float rotTarget = Mathf.Atan2(move.movement.x, move.movement.z);
        float currRot   = Mathf.Atan2(transform.forward.x, transform.forward.z);

        rotTarget = Mathf.Rad2Deg * rotTarget;
        currRot   = Mathf.Rad2Deg * currRot;

        float rotationdif = Mathf.DeltaAngle(rotTarget, currRot);

        rotationdif = Mathf.Clamp(rotationdif, -move.max_rot_acceleration, move.max_rot_acceleration);

        float acceleration = rotationdif * move.max_rot_acceleration;

        if (Mathf.Abs(rotationdif) < slow_angle)
        {
            rotationdif *= time_to_target;

            if (Mathf.Abs(rotationdif) < min_angle)
            {
                rotationdif = 0.0f;
            }

            acceleration = rotationdif * move.max_rot_acceleration;
        }

        move.AccelerateRotation(acceleration);
    }
Пример #3
0
    // Update is called once per frame
    void Update()
    {
        // Orientation we are trying to match
        float delta_angle = Vector3.SignedAngle(transform.forward, move.target.transform.forward, new Vector3(0.0f, 1.0f, 0.0f));


        float diff_absolute = Mathf.Abs(delta_angle);

        if (diff_absolute < min_angle)
        {
            move.SetRotationVelocity(0.0f);
            return;
        }

        float ideal_rotation_speed = move.max_rot_speed;

        if (diff_absolute < slow_angle)
        {
            ideal_rotation_speed *= (diff_absolute / slow_angle);
        }

        float angular_acceleration = ideal_rotation_speed / time_to_accel;

        //Invert rotation direction if the angle is negative
        if (delta_angle < 0)
        {
            angular_acceleration = -angular_acceleration;
        }

        move.AccelerateRotation(Mathf.Clamp(angular_acceleration, -move.max_rot_acceleration, move.max_rot_acceleration), priority);
    }
Пример #4
0
    // Update is called once per frame
    void Update()
    {
        // TODO 7: Very similar to arrive, but using angular velocities
        // Find the desired rotation and accelerate to it
        // Use Vector3.SignedAngle() to find the angle between two directions

        Vector3 targetDir = move.target.transform.position - transform.position;
        Vector3 forward   = transform.forward;
        float   angle     = Vector3.SignedAngle(forward, targetDir, Vector3.up);

        float angularRotation = angle;

        if (Mathf.Abs(angle) < slow_angle)
        {
            float idealAngle = angle / time_to_target;
            angularRotation = idealAngle;

            if (Mathf.Abs(angle) < min_angle)
            {
                angularRotation = 0.0f;
                move.SetRotationVelocity(angularRotation);
            }
        }

        move.AccelerateRotation(angularRotation);
    }
Пример #5
0
    // Update is called once per frame
    void Update()
    {
        // Orientation we are trying to match
        float my_orientation     = Mathf.Rad2Deg * Mathf.Atan2(transform.forward.x, transform.forward.z);
        float target_orientation = Mathf.Rad2Deg * Mathf.Atan2(move.target.transform.forward.x, move.target.transform.forward.z);
        float diff = Mathf.DeltaAngle(my_orientation, target_orientation);         // wrap around PI

        float diff_absolute = Mathf.Abs(diff);

        if (diff_absolute < min_angle)
        {
            move.SetRotationVelocity(0.0f);
            return;
        }

        float ideal_rotation = 0.0f;

        if (diff_absolute > slow_angle)
        {
            ideal_rotation = move.max_rot_velocity;
        }
        else
        {
            ideal_rotation = move.max_rot_velocity * diff_absolute / slow_angle;
        }

        float angular_acceleration = ideal_rotation / time_to_target;

        if (diff < 0)
        {
            angular_acceleration = -angular_acceleration;
        }

        move.AccelerateRotation(Mathf.Clamp(angular_acceleration, -move.max_rot_acceleration, move.max_rot_acceleration), priority);
    }
    // Update is called once per frame
    void Update()
    {
        // TODO 7: Very similar to arrive, but using angular velocities
        // Find the desired rotation and accelerate to it
        // Use Vector3.SignedAngle() to find the angle between two directions

        float desired_rot = Vector3.SignedAngle(Vector3.forward, move.movement, Vector3.up);
        float current_rot = move.transform.localRotation.eulerAngles.y;
        float final_rot   = desired_rot - current_rot;

        //Delete additional loops
        final_rot %= 360;

        if (Mathf.Abs(final_rot) > 180)
        {
            float sign = Mathf.Sign(final_rot);
            final_rot  = 360 - Mathf.Abs(final_rot);
            final_rot *= -sign;
        }

        if (final_rot < slow_angle)
        {
            final_rot /= slow_angle;
        }

        if (Mathf.Abs(final_rot) <= min_angle)
        {
            move.SetRotationVelocity(0f);
        }
        else
        {
            move.AccelerateRotation(final_rot);
        }
    }
Пример #7
0
    // Update is called once per frame
    void Update()
    {
        Vector3 diff = transform.forward;
        Vector3 axis;

        axis.x = 0.0f;
        axis.y = 1.0f;
        axis.z = 0.0f;
        Vector3 current = move.current_velocity;
        float   desired = Vector3.SignedAngle(diff, current, axis);
        //move.SetRotationVelocity(desired);
        float accel = (desired - move.current_rotation_speed) * Time.deltaTime * 3;

        //desired = desired.normalized * move.max_mov_speed * slow_distance;
        if (desired < 0)
        {
            desired = -desired;
        }
        if (desired < min_angle)//desired absolut
        {
            move.SetRotationVelocity(0.0f);
        }
        else
        {
            Mathf.Clamp(accel, move.max_rot_acceleration, -move.max_rot_acceleration);
            move.AccelerateRotation(accel);
        }
    }
Пример #8
0
    // Update is called once per frame
    void Update()
    {
        // TODO 4: As with arrive, we first construct our ideal rotation
        // then accelerate to it. Use Mathf.DeltaAngle() to wrap around PI
        // Is the same as arrive but with angular velocities

        float targetAngle = Mathf.Atan2(move.movement.x, move.movement.z) * Mathf.Rad2Deg;
        float currAngle   = Mathf.Atan2(transform.forward.x, transform.forward.z) * Mathf.Rad2Deg;
        float deltaAngle  = Mathf.DeltaAngle(currAngle, targetAngle);

        float angularRotation = deltaAngle;

        if (Mathf.Abs(deltaAngle) < slow_angle)
        {
            float idealAngle = deltaAngle / time_to_target;
            angularRotation = idealAngle;

            if (Mathf.Abs(deltaAngle) < min_angle)
            {
                angularRotation = 0.0f;
                move.SetRotationVelocity(angularRotation);
            }
        }

        move.AccelerateRotation(angularRotation);
    }
Пример #9
0
    // Update is called once per frame
    void Update()
    {
        // TODO 4: As with arrive, we first construct our ideal rotation
        // then accelerate to it. Use Mathf.DeltaAngle() to wrap around PI
        // Is the same as arrive but with angular velocities


        float target_degrees  = Mathf.Atan2(move.movement.x, move.movement.z) * Mathf.Rad2Deg;
        float current_degrees = Mathf.Atan2(transform.forward.x, transform.forward.z) * Mathf.Rad2Deg;
        float delta           = CalcShortestRot(current_degrees, target_degrees);


        float ideal_angle = delta;//= Mathf.MoveTowardsAngle(current_degrees, target_degrees, move.max_rot_acceleration);

        // angle = Mathf.LerpAngle(target_degrees, current_degrees, Time.time);

        if (Mathf.Abs(delta) < slow_angle && Mathf.Abs(delta) > min_angle)
        {
            ideal_angle *= current_degrees / target_degrees;
        }
        else if (Mathf.Abs(delta) < min_angle)
        {
            ideal_angle   = 0;
            move.rotation = 0;
        }

        if (Mathf.Abs(ideal_angle) > move.max_rot_acceleration)
        {
            Mathf.Clamp(ideal_angle, -move.max_rot_acceleration, move.max_rot_acceleration);
        }


        move.AccelerateRotation(ideal_angle);

        /*float target_degrees = Mathf.Atan2(move.movement.x, move.movement.z) * Mathf.Rad2Deg;
         * float current_degrees = Mathf.Atan2(transform.forward.x, transform.forward.z) * Mathf.Rad2Deg;
         * float delta = Mathf.DeltaAngle(target_degrees, current_degrees);
         *
         * if(Mathf.Abs(delta) < min_angle)
         *  move.SetRotationVelocity(0.0f);
         * else
         *  move.SetRotationVelocity(-delta);*/
    }
    // Update is called once per frame
    void Update()
    {
        // TODO 7: Very similar to arrive, but using angular velocities
        // Find the desired rotation and accelerate to it
        // Use Vector3.SignedAngle() to find the angle between two directions
        float desired_rotation = Vector3.SignedAngle(Vector3.forward, move.movement, Vector3.up);
        float curr_rotation    = move.tank_base.localRotation.eulerAngles.y;

        float final_rotation = desired_rotation - curr_rotation;

        //Delete additional loops
        final_rotation %= 360;

        //Prioritize the lowest angle on a circle
        //Example: Between be 350º and -10º we'll choose -10º
        if (Mathf.Abs(final_rotation) > 180)
        {
            float sign = Mathf.Sign(final_rotation);
            final_rotation  = 360 - Mathf.Abs(final_rotation);
            final_rotation *= -sign;
        }

        if (final_rotation < slow_angle)
        {
            final_rotation /= slow_angle;
        }

        if (Mathf.Abs(final_rotation) <= min_angle)
        {
            move.SetRotationVelocity(0f);
        }
        else
        {
            move.AccelerateRotation(final_rotation);
        }
    }
Пример #11
0
    public bool Steer(Vector3 target)
    {
        //    if (!move)
        //        move = GetComponent<Move>();
        //    else
        //    {
        //        float delta_angle = Vector3.SignedAngle(transform.forward, move.GetPriorityVelocity(), new Vector3(0.0f, 1.0f, 0.0f));

        //        float diff_absolute = Mathf.Abs(delta_angle);

        //        if (diff_absolute < min_angle)
        //        {
        //            move.SetRotationVelocity(0.0f);
        //            return;
        //        }

        //        float ideal_rotation_speed = move.max_rot_speed;

        //        if (diff_absolute < slow_angle)
        //            ideal_rotation_speed *= (diff_absolute / slow_angle);

        //        float angular_acceleration = ideal_rotation_speed / time_to_accel;

        //        //Invert rotation direction if the angle is negative
        //        if (delta_angle < 0)
        //            angular_acceleration = -angular_acceleration;

        //        move.AccelerateRotation(Mathf.Clamp(angular_acceleration, -move.max_rot_acceleration, move.max_rot_acceleration), priority);
        //    }

        //}

        float delta_angle = Vector3.SignedAngle(transform.forward, new Vector3(target.x - transform.position.x, 0, target.z - transform.position.z), new Vector3(0.0f, 1.0f, 0.0f));

        float diff_absolute = Mathf.Abs(delta_angle);

        if (diff_absolute < min_angle)
        {
            move.rotation = 0;
            return(true);
        }

        float ideal_rotation_speed = move.max_rot_speed;

        if (diff_absolute < slow_angle)
        {
            ideal_rotation_speed *= (diff_absolute / slow_angle);
        }

        float angular_acceleration = ideal_rotation_speed / time_to_accel;

        //Invert rotation direction if the angle is negative
        if (delta_angle < 0)
        {
            angular_acceleration = -angular_acceleration;
        }

        move.AccelerateRotation(/*Mathf.Clamp(*/ angular_acceleration /*, -move.max_rot_acceleration, move.max_rot_acceleration)*/, priority);

        return(false);
    }
Пример #12
0
    // Update is called once per frame
    void Update()
    {
        // TODO 7: Very similar to arrive, but using angular velocities
        // Find the desired rotation and accelerate to it
        // Use Vector3.SignedAngle() to find the angle between two directions

        // Orientation we are trying to match

        //----------Marc Garrigó Code ----------//
        float my_orientation     = Mathf.Rad2Deg * Mathf.Atan2(transform.forward.x, transform.forward.z);
        float target_orientation = Mathf.Rad2Deg * Mathf.Atan2(move.target.transform.forward.x, move.target.transform.forward.z);
        float diff = Mathf.DeltaAngle(my_orientation, target_orientation); // wrap around PI

        float diff_absolute = Mathf.Abs(diff);

        if (diff_absolute < min_angle)
        {
            move.SetRotationVelocity(0.0f);
            return;
        }

        float ideal_rotation = 0.0f;

        if (diff_absolute > slow_angle)
        {
            ideal_rotation = move.max_rot_speed;
        }
        else
        {
            ideal_rotation = move.max_rot_speed * diff_absolute / slow_angle;
        }

        float angular_acceleration = ideal_rotation / time_to_target;

        if (diff < 0)
        {
            angular_acceleration = -angular_acceleration;
        }

        move.AccelerateRotation(Mathf.Clamp(angular_acceleration, -move.max_rot_acceleration, move.max_rot_acceleration));



        //----------Carlos Code 1----------//
        //Vector3 targetDir = move.target.transform.position - transform.position;
        //Vector3 forward = transform.forward;
        //float angle = Vector3.SignedAngle(forward, targetDir, Vector3.up);

        //float angularRotation = angle;
        //move.SetRotationVelocity(angularRotation * 5);


        //----------Carlos Code 2----------//
        //Vector3 targetDir = move.target.transform.position - transform.position;
        //Debug.DrawRay(transform.position, targetDir, Color.green);

        //float angle = Mathf.Atan2(targetDir.z, targetDir.x) * Mathf.Rad2Deg - 90;
        //Debug.Log("Angle: " + angle);

        //move.AccelerateRotation(angle);

        //Quaternion angleAxis = Quaternion.AngleAxis(-angle, Vector3.up);

        //transform.rotation = Quaternion.Slerp(transform.rotation, angleAxis, Time.deltaTime * 10);
    }