Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

(3D Platformer) Rotating with a mesh's rotation

Discussion in 'Physics' started by Shizuku_Mizuki, Dec 2, 2018.

  1. Shizuku_Mizuki

    Shizuku_Mizuki

    Joined:
    Nov 5, 2017
    Posts:
    17
    I've seen a ton of people asking about similar questions, but none of the answers seem to work for my situation, nor do they seem to explain why their solution works, so it's hard for me to even come up with a solution on my own.

    To be frank, I'm really bad with Geometric and Trigonometric math. However, the solution to this problem solely relies on it. In short, what I want is to be able to rotate a Rigidbody's X and Z axes to match a slanted platform in a fully 3D platformer.

    So, if I were to move from left to right in the below image and get on the platform, I want my rigidbody to be able to rotate so that it will "run up" the platform. And, in the event that they turn around, I want my rigidbody to now be "running down" the platform.
    example platforms.PNG
    How would I be able to accomplish that? I don't necessarily want to be spoonfed code, if that's possible.
     
  2. Shizuku_Mizuki

    Shizuku_Mizuki

    Joined:
    Nov 5, 2017
    Posts:
    17
    Found the answer by asking on the GoTo:GameDev Discord and I felt I should share it. Apparently this has been posted elsewhere and I just didn't find it due to me not using the right search terms necessarily.
    https://forum.unity.com/threads/quaternion-rotation-along-normal.22727/
    solution.PNG

    In short, you're raycasting downwards towards a surface, using the Quaternion.FromToRotation() function and using both your up vector and the normal of the surface, and then multiplying that by your rotation so that you can still rotate your rigidbody along the surface.

    Hope this helps anyone else who happens to find this thread!
     
    MJBarr likes this.