Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Turning based on camera, how to determine if I'm going left or right?

Discussion in 'Scripting' started by chainsawpenguin, Aug 20, 2019.

  1. chainsawpenguin

    chainsawpenguin

    Joined:
    Sep 28, 2014
    Posts:
    107
    Normally, turning is controlled by the horizontal input, and I can use the value of that to determine if I'm turning to the right (Input.GetAxis("Horizontal") > 0) or to the left ( <0). However, my character is turning to face the direction that the camera is pointing while I'm aiming a weapon. This means that the character can rotate on the spot to either the left or the right without any horizontal input at all.

    How can I determine if my transform.forward from this frame is to the left or right of the transform.forward from the previous frame?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,006
    I don't fully understand the context of why this is needed, but you should be able to use camera.transform.TransformDirection(transform.forward) from both frames and compare those X values directly.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,277
    Vector3.SignedAngle seems like it should work for this.

    Note, of course, that you can't tell the difference between rotating 90 degrees left and rotating 270 degrees right just by looking at the start and end points. This probably won't matter unless your character is spinning super fast.

    Presumably you could also rely on whatever system you are using to control the camera's rotation. That must be set somehow.

    How does comparing the X values of the vectors help you?
     
  4. chainsawpenguin

    chainsawpenguin

    Joined:
    Sep 28, 2014
    Posts:
    107
    Vector3.SignedAngle looks like what I'm hunting around for! Thanks!

    For a fuller explanation, I want to know when I should pass a signal to my animator to play a turning rotation.

    I could also pass the input that causes the rotation of the camera, and I am doing that when the user is inputting a horizontal rotation.

    What's tripping me up is when the player is aiming, and the camera is automatically tracking an object that is moving through space, then I need to know if the camera is rotating to the left or the right (or really, when my player is rotating left or right...) ...which is my original problem.

    I'll give this a shot! Thanks!
     
unityunity