Search Unity

Help with Math: LookRotation + x = Velocity

Discussion in 'Scripting' started by MastermindInteractive, Feb 1, 2020.

  1. MastermindInteractive

    MastermindInteractive

    Joined:
    Jun 13, 2014
    Posts:
    89
    I'm trying to move an object to a specific position while having its rotation continue to point toward a different location. I'm not using NavMesh. I need to update the velocity to account for their rotation which I want to look always point toward a target. I'm not sure about the math that is needed to calculate this velocity.

    Object Rotation + Movement Angle = Velocity? <shrug>

    upload_2020-2-1_8-42-21.png
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    I'm not quite sure i understand your question.
    An object can move on some axis and look to something else. If your velocity is (1,0,0) in world space, then this is independant of where you look. To make your object look in some direction you could just use LookAt().

    If you are asking about how to apply a force to a rigidbody that rotates it but also keeps its velocity the same, then i dont think i have a good answer, but i would like to hear what this is required for in your game.
     
  3. diXime

    diXime

    Joined:
    Oct 2, 2018
    Posts:
    162
    Hello,
    Velocity is a change of position through time. Like Yoreki said, it is completely independent of where it is "looking". (if you want an example, the reason you need to be careful when steering a car is that once it is launched to a direction even the wheels can have troubles reorienting the motion)
    If you're using RigidBody, in my opinion the problem is rather trivial, you set a velocity it goes there. If your problem comes that you're trying to translate to a direction expressed locally, i'd rather use a destination (your specific location) instead of an angle (subtract position to direction to get the vector between the two points and normalize it, you'll get a heading vector). Your velocity vector is obtained by multiplying by an amount, the speed, if the word is correct. To get the angle between two 3D vectors (like object rotation and movement angle), you'll need a dot product, if you really need to know the angle between the translation and the look orientation.
    Simple reminder from the physics, velocity is conserved. So no resetting in Update, only acceleration is.
     
    Last edited: Feb 2, 2020