Search Unity

Vector3.RotateTowards half way ?

Discussion in 'Scripting' started by Aedous, Jul 30, 2014.

  1. Aedous

    Aedous

    Joined:
    Jun 20, 2009
    Posts:
    244
    Hi guys!

    Is it possible to use the function Vector3.RotateTowards to return a rotation direction that is half way between the full rotation result ?

    I'm using this function to achieve a good result Quaternion.AngleAxis( degrees, Vector3.forward ).
    Hopefully this picture explains exactly what I'm trying to achieve:
    Black line represents the AI's moving direction.
    Scenario A - AI should rotate towards the red line ( in this case the degrees will be positive )
    scenarioone.png
    Scenario B - AI should rotate towards the red line ( in this case the degrees will be negative )
    scenariotwo.png


    I've tried to negate the degrees depending on the x direction of the AI's moving direction ( which is the black line ) but this doesn't always give the correct result as sometimes the AI will rotate away from the target.

    The only function I can think of that actually works quite well is Vector3.RotateTowards, but I would rather I could specifically state how much in degrees it should rotate rather than a full rotation.

    Hope anyone can help me with this problem :).
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Yeah, you just need to know how far you want the RotateTowards to go. I think you can use Vector3.Angle to work that out. Keep in mind that Vector3.Angle takes a direction vector, not a position vector.
     
  3. Aedous

    Aedous

    Joined:
    Jun 20, 2009
    Posts:
    244
    Thanks for the reply, I tried your answer and no matter what value I pass into the RotateTowards function I always get a direction pointing directly at the target.

    Just to make sure I'm not entering anything wrong here's what I'm passing in
    Vector3 newDirection = Vector3.RotateTowards(movingDirection, targetDirection, degrees, 0f );

    No matter what value I pass into the degrees parameter I always get a rotation facing the target ?
     
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Without knowing the actual values I really have no idea what's going on there. All that shows me is their names.
     
  5. Aedous

    Aedous

    Joined:
    Jun 20, 2009
    Posts:
    244
    Ah sorry about that,
    scenarioone.png
    Vector3.RotateTowards(movingDirection, targetDirection, degrees, 0f )
    movingDirection is the black line the direction the AI is moving
    targetDirection is the direction from the AI to the target
    degrees is the amount I want to turn ( eg 45 )

    If that makes things a lot clearer, I read the documentation and passed in positions by just creating a temporary value which stores the hit point of the black line to the blue wall, and for the target position by using the targetDirection * (distance between AI and point it hit on the wall )