ApplyRotation Change

Hi,

I am wondering why the method extraction of ApplyRotation changed the focus of the ‘-’ (negative) in the equation from ‘Vector3.forward’ to ‘rotationThrust’ and therefore ‘rotationThisFrame’.

Is the change from transform to force required?

Thanks

Hi,

Have you already tried to remove the minus to see what happens?

Yep, maybe I didn’t ask the question correctly.

When we are taught in the beginning to set up the ProcessRotation() it goes as follows:

if (Input.GetKey(KeyCode.A))

transform.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);

else if (Input.GetKey(KeyCode.D))

transform.Rotate(-Vector3.forward * rotationThisFrame * Time.deltaTime);

  • The focus of the change in ‘-’ is on the Vector3.forward piece of the equation.
  • When we are taught the ApplyRotation(rotationThrust) to simplify the code, we get:

void ProcessRotation()

if (Input.GetKey(KeyCode.A))
ApplyRotation(rotationThrust);

else if (Input.GetKey(KeyCode.D))
ApplyRotation(-rotationThrust);

void ApplyRotation(float rotationThisFrame)
t.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);

  • The focus is now on the second piece of the equation (rotationThisFrame), not the Vector3.forward.

I’m not sure if I understood you correctly, so please correct me if the following did not help:

The ApplyRotation method recieves “a value”, and we use that value in the transform.Rotate method. rotationThisFrame is just a local variable. We do not use the minus here because there is no way to determine the direction based on rotationThisFrame.

In the ProcessRotation method, we know when we want to change the direction. And we pass on that value to the ApplyRotation method.

We could have written this in a slightly different way. Keep in mind that the following method less inefficient than the original ProcessRotation method due to the additional if-statement. This is just an example to visualise an idea:

void ProcessRotation()
{
   if (!Input.GetKey(KeyCode.A) ||  !Input.GetKey(KeyCode.D)) { return; }

   Vector3 thrust = rotationThrust;
   if (Input.GetKey(KeyCode.D)) { thrust *= -1; }

   ApplyRotation(thrust);
}

It does not matter where you “flip” the value of rotationThrust. We pass on the value of thrust to the ApplyRotation method, not the variable named thrust.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms