Rotation Priority in C# Unity

Viewed 39
public float turnSmoothTimeFire = 0.0001f;
float turnSmoothVelocity;
     
void Update()
    {
            float horizontal = Input.GetAxisRaw("Horizontal");
            float vertical = Input.GetAxisRaw("Vertical");
            Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;

    if (Input.GetButton("Fire1"))
    {
        float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
        float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTimeFire);
        transform.rotation = Quaternion.Euler(0f, angle, 0f);
    }
}

I have another if statement with the exact same setup (just the variables are called differently) for turning my player in the direction of moving, and i was wondering if i can prioritize my rotation. Currently, my player rather turns in the direction i am moving instead of the direction i am fireing, and i would like it the other way around. Help is greatly apprectiated.

1 Answers

You can make the Fire check first and return from the method before checking the movement logic.

Another approach would be to take both into account by adding the angle calculated for movement and the one for the aiming.

It depends on your requirements.

You can also check the Transform.Rotate method, that is recommended for use instead of manual rotation setting.

Related