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

How do I move character in relation to camera WITHOUT root motion?

Discussion in 'Scripting' started by Bryan77, Oct 20, 2015.

  1. Bryan77

    Bryan77

    Joined:
    Jun 9, 2014
    Posts:
    24
    Hello. I'm trying to make a 3D Action/Platformer game similar to Banjo Kazooie. I've been having a hard time getting the character movement that I want. Every tutorial I see uses root motion, but I always found root motion to feel very clunky.


    http://www.newtonians3d.blogspot.in/2014/09/simple-third-person-character-movement.html
    This tutorial looks promising on how to get 3rd person movement relative to the camera. However, he sets anim speed to 1 (which is a condition to make him walk) that is how he gets his character moving. Meaning he is using root motion. How do I get him to walk without root motion in the direction that I want?

    I also don't simply want to move my character "forward" while he is turning, as that will make him move in a circular fashion. If he is facing right, and I press left, I want him to start moving left, as he turns to face in that direction.
    root motion (as well as him moving "forward") would have him move in a circular fashion and not be what I want.

    I would really appreciate it if someone can help me out here. Also, I'm an animator rather than a programmer, so some programming things are confusing to me. I just want to get my guy running around and jumping on screen so I can spend my time creating content, and better animations. This movement thing is a real roadblock for me.
     
    Last edited: Oct 20, 2015
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    The answer to your question is to alter transform.position. For example:
    Code (csharp):
    1. void Update() {
    2. transform.position = transform.position + new Vector3(Input.GetAxis("Horizontal") * movemendSpeed * Time.deltaTime, 0f, Input.GetAxis("Vertical") * movemendSpeed * Time.deltaTime);
    3. }
    4.  
    I'm honestly puzzled, however, as to why you don't want to use root motion. You say it's "clunky", but I'm not sure what that means. If you are an animator and not a coder, root motion is far easier to get used to and work with. In fact, even as a coder who can easily work around root motion, I still much prefer it - and it seems like you'll have a much harder time working around it than I would.

    So it may be better to address what your problems are with root motion as opposed to avoiding it outright.
     
  3. Bryan77

    Bryan77

    Joined:
    Jun 9, 2014
    Posts:
    24
    Thank you for your reply. I put your script onto a cube, and all it does is move relative to world space. I want "w" to always make you move forward in the direction the camera is facing. And "a" to make you go left of the way the camera is facing.

    What I mean by "clunky" is that it feels bad. If you load up Unity's default 3rd person controller with Ethan, and play around with him you will see what I mean. Make him face right, and then press "a". He plays a turn step animation, and only once he is facing left does he proceed to run.

    What I want is for him to start moving left, and while he is moving left, he is also turning. Technically it isn't accurate because his feet are sliding, but it feels better which is more important. Also although his feet slide a bit, you only notice it if you are looking for it. If I wanted to get rid of it, I can throw in a turn animation that plays.

    The 2nd problem is that when you are running left, and try to make him run away form the camera by pressing "w" he slows down. This I do not want. I want him to run a consistant speed. Any hickup in the animation is going to severely alter the way he moves.

    Which brings me to the 3rd problem. Since animation is what drives movement, there is no way I can do prototyping with cubes (which is what I read that a lot of the big developers start off with to get fun movement, and then later add in the models with animations)

    Also, for root motion, I need to animate him running forward. Doing that is much harder than animating a run cycle in place, as I can't see what it looks like looped because he will constantly be teleporting back to the beginning. This isn't good for animating. Also, if I change any of the animations, that will screw everything up. I would like to get my game working using some standin animations, and then when I have time, I would like to go back in and make the animations look better.

    Ethan turns, and then runs. I don't want that. I also don't want him to snap instantly to the correct rotation. Using root motion, I would have to have him run, and turn at the same time which will result in him moving in a small circle.

    Contrast all that with games like Banjo Kazooie, Super Mario 64, Ratchet and Clank, Psychonauts, etc... Yes there is foot sliding, but its not that bad, and the games feel good to control.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Maybe true, but keep in mind, big developers also have programmers working for them. Not every development strategy that works for them is going to work for you.

    Ah, I see. You need to start with Camera.main.transform.forward and .right. Multiple those values by the vertical and horizontal axis inputs (respectively), then add the vectors together. Finally, set that vector.y to 0 to flatten it. This vector can then be multiplied by Time.deltaTime and your movement speed, and added to your position.

    Code (csharp):
    1. Vector3 forwardMovement = Camera.main.transform.forward * Input.GetAxis("Vertical");
    2. Vector3 rightMovement = Camera.main.transform.right * Input.GetAxis("Horizontal");
    3. Vector3 totalMovement = forwardMovement + rightMovement;
    4. totalMovement.y =0f;
    5. transform.position += totalMovement * Tiem.deltaTime * movementSpeed;
     
  5. Bryan77

    Bryan77

    Joined:
    Jun 9, 2014
    Posts:
    24
    Thank you StarManta! That is exactly what I was looking for!

    Now I just need to figure out how to make him move slower initially when he is already at a stop, so that he accelerates into his normal speed. Then add in jumping. I might start a new thread for those, as they are different questions entirely. Thank you!