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

Walking on arbitrary surfaces and relatively to the camera

Discussion in 'Scripting' started by 06hypersonic60, Aug 1, 2015.

  1. 06hypersonic60

    06hypersonic60

    Joined:
    Apr 19, 2015
    Posts:
    10
    Hey guys. This is my first post in the forums.
    I'm making a sonic game and have been struggling with some things for a while.
    The most annoying problem Im facing as the title states, is walking on slopes ( walls and loop de loops included )

    My player movements explained:
    I have a vector3 I called direction that has CurrentSpeedX,0,CurrentSpeedZ as components.
    CurrentSpeedX and CurrentSpeedZ increase / decrease when you hold or release the V/H axes buttons.

    Then I use a new vector called lastDirection and make it equal to Camera.main.transform.TransformDirection(direction); when one of the axes is being pressed.
    Sonic is then rotated using LookRotation(lastDirection,alignhit.normal) with alignhit.normal the ground normal (using a raycast of course)
    Lastly, I move sonic using Rigidbody.velocity on his transform.forward*CurrentSpeed with CurrentSpeed that direction movement magnitude.
    It works perfectly on flat surfaces, but for some reason on slopes, it'll only work when facing / moving left or right.
    A user in UA (always sunny) told me to use ProjectToPlane, thinking I wouldnt move on arbitrary surfaces.
    When I did that, sonic did align normally until he reached 90° between him and the ground. He started then either running to his left or right.

    Here's the link to the post. You may understand a bit more if you read his long comment:
    http://answers.unity3d.com/questions/1018894/player-aligned-to-ground-only-when-lookingmoving-l.html
    And here is a video of how the controls are supposed to be:


    After looking a bit more around, I found out about Vector3.Cross and someone actually making a sonic game on uniy
    ( this:
    ) tried helping me to get it to work. But it never did.
    Sometimes it made my movements jerky, and when it didnt, the same alignment issue was there.

    Ive been trying to fix this for a whlie now without luck.
    So i hope someone can lead me to the right path.

    Thanks in advance :D