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. Dismiss Notice

Wall running squirrel

Discussion in 'Scripting' started by TheAmazingB74, Jan 13, 2022.

  1. TheAmazingB74

    TheAmazingB74

    Joined:
    Aug 28, 2015
    Posts:
    14
    Looking for a little help on this. I'm making a parkour-ish game w a squirrel as the main character. I using a 3rd person set up. I'm having issues w getting him to face the direction he is traveling while on walls. On the ground, this works fine
    if (grounded ) {
    Vector3 forwardDir = new Vector3(body.velocity.x, 0f, body.velocity.z);
    Quaternion rotation = Quaternion.LookRotation(forwardDir, contactNormal);
    transform.rotation = rotation;
    Debug.Log("Gound V " + body.velocity);
    }

    I've tried several things to fix it similar to this
    if (climbing) {
    Vector3 forwardDir = new Vector3(body.velocity.x, 0f, body.velocity.y);
    Quaternion rotation = Quaternion.LookRotation(forwardDir, climbNormal);
    transform.rotation = rotation;
    Debug.Log("Wall V" + body.velocity);
    }

    What I am trying to do is get it so the transform.up faces away from the wall AND the character faces the direction he is going. Hers is a simple video to show the problem
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Check out some tutorials on spherical gravity planet walking, like the one from Sebastian Lague.

    Above you are just taking the normal and calculating a direction.

    I think the steps you want are to calculate small rotations to go from the normal last frame and to the normal this frame.

    You would then use those small rotations in turn to rotate your player's rotation, which will preserve heading.

    So as you hit the wall, the floor normal would be up, the wall normal would be back, and you would compute a "rotate backwards 90 degrees" rotation from that for the frame you hit the wall. Then you would rotate your player's ACTUAL rotation (regardless of what it was) backwards by 90 degrees (quaternion multiplication), which will turn his snout going up-wall.
     
  3. TheAmazingB74

    TheAmazingB74

    Joined:
    Aug 28, 2015
    Posts:
    14
    I'll have to take a look at the tutorial you mentioned. This is turning out to be a little more complicated than I expected. Thanks!
     
    Kurt-Dekker likes this.