Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to rotate rigidbody's velocity with object's rotation

Discussion in 'Scripting' started by Kinosei, Feb 8, 2020.

  1. Kinosei

    Kinosei

    Joined:
    Dec 3, 2015
    Posts:
    7
    I have a character controller that climb walls like if it was ground, so the up vector (Y) won't always be the world up.

    I also have a trigger that teleports the character to a certain place rotating it if needed (any direction). But only rotating it, the velocity won't go together, it keeps the momentum for the previous direction, world related.

    How do I keep the momentum but to the correct relative direction? It needs to change the velocity direction according to the rotation, but I can't figure it out how.

    Here's the transport code:

    Code (CSharp):
    1. public void ChangeTransform(Transform newTransform)
    2. {
    3.     transform.position = newTransform.position;
    4.     transform.eulerAngles = newTransform.eulerAngles;
    5.     transform.localScale = newTransform.localScale; // This is not needed right now
    6. }
     
  2. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,469
  3. Kinosei

    Kinosei

    Joined:
    Dec 3, 2015
    Posts:
    7
    Thanks. It seems similar to my problem, but I'm not entirely sure what to use from the code. It seems it is for a rotation over time according to inputs, and I don't see anything about rigidbody's velocity. I search for some way to rotate the velocity vector according to a instant rotation from the object. I think I should use
    Quaternion target = Quaternion.LookRotation( relative_direction, MC.transform.up );
    but I'm not sure how, as velocity is a Vector3