Camera attached to head bone make the character able to walk on any axis and in the air?

When I attached my camera to my characters head bone so the camera moves up and down with the character it made it so when I move the character he is able to walk/run on any axis, basically he is able to fly. He can walk up down left right underground or in mid-air. I have two sets of code:

CameraControl Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class camMouseLook : MonoBehaviour
{

    public Transform characterTransform;
    public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    public RotationAxes axes = RotationAxes.MouseXAndY;
    public float sensitivityX = 15F;
    public float sensitivityY = 15F;
    public float minimumX = -360F;
    public float maximumX = 360F;
    public float minimumY = -60F;
    public float maximumY = 60F;
    float rotationY = 0F;

    void Update()
    {
        if (axes == RotationAxes.MouseXAndY)
        {
            float rotationX = characterTransform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

            rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
            rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

            characterTransform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
        }
        else if (axes == RotationAxes.MouseX)
        {
            characterTransform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
        }
        else
        {
            rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
            rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

            characterTransform.localEulerAngles = new Vector3(-rotationY, characterTransform.localEulerAngles.y, 0);
        }
    }


}

Character Control Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterControl : MonoBehaviour
{

    private float speed = 0;
    public float wSpeed;
    public float rSpeed;

    static Animator anim;

    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;

        anim = GetComponent<Animator>();
    }

    void Update()
    {
        float z = Input.GetAxis("Vertical") * speed;
        float y = Input.GetAxis("Horizontal") * speed;

        transform.Translate(0, 0, z);
        transform.Translate(y, 0, 0);

        if (Input.GetKeyDown("escape"))
            Cursor.lockState = CursorLockMode.None;

        if (Input.GetKey(KeyCode.W))
        {
            speed = rSpeed;
            anim.SetBool("isWalk", false);
            anim.SetBool("isIdle", false);
            anim.SetBool("isRun", true);
        }
        if (Input.GetKey("left shift") && Input.GetKey(KeyCode.W))
        {
            speed = rSpeed;
            anim.SetBool("isWalk", false);
            anim.SetBool("isIdle", false);
            anim.SetBool("isRun", true);
        }
        else if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.A) && !Input.GetKeyUp("left shift"))
        {
            speed = wSpeed;
            anim.SetBool("isWalk", true);
            anim.SetBool("isIdle", false);
            anim.SetBool("isRun", false);
        }
        //Idle
        else
        {
            anim.SetBool("isWalk", false);
            anim.SetBool("isIdle", true);
            anim.SetBool("isRun", false);
        }
    }

}

Thanks for any help!

One thing I just thought of was do your animations apply any sort of root motion at all? I mean, you’re moving your player by this part below already… but I wonder if it’s additional movement from the animation that’s causing the effect you’re seeing.

float z = Input.GetAxis("Vertical") * speed;
float y = Input.GetAxis("Horizontal") * speed;

transform.Translate(0, 0, z);
transform.Translate(y, 0, 0);

Maybe try turning off the root motion, but I’m not sure if that would make you lose your head bob affect that you initially wanted. If so, I think you can do a combination of the other suggestions … ensure use gravity on your rigidybody is on… try locking the Y axis for position.

before even doing that, you could even comment the code above out and see if your player is moving due to the animation. If so, that is where the problem is, I think.

Hope that helps!!!

Edit: just sent you a PM… I think in the other thread, I completely misunderstood what you were telling me about the body rotating, possibly. Long story short, you’ll probably want the upper body to move with the mouse up/down, and your entire body to move based on mouse left/right. This may solve your entire problem. If so, I apologize for being such a dummy here!

1 Like

yes! I want the upper body (head) to move up and down and the whole body to move left and right. How do I achieve this its still doing this weird walking through the air thing.

Oops my bad I used the new code you gave me in the PM and it stopped the weird walking through the air but I cant look up and down, I am only able to look left and right.

Hmm, I’m doing this in a test project, and I see the same effect. In my case, and I’m guessing yours, it’s because of the animator. I assume it’s preventing chest movement in play mode. You can see this too by just clicking on the top level of your character, and temporarily disabling or setting to none the Animator controller. then you’ll be able to rotate. I’m just trying to figure the next work around… sorry, never built an FPS before, so this is a complete learning experience for me too. lol

Edit: interesting… just simply changing avatars fixed this… that gives me a hint… update coming soon…

Yea I just temporarily disabled the animator and it worked, weird. What did you change the avatar too?

Well, I have been testing with an asset package i got off the store called Simple Characters, and it came with a couple of avatars. One works, one doesn’t. I’ll see if I can find something free that will work. You can set the Avatar to none instead of the controller, and the movement will still work. But I’m not sure what happens to your animations though.

To be honest, I really don’t have enough knowledge behind how the avatar works exactly. I tried to compare settings, but they look the same. Not sure if it’s something that can’t be seen in unity that is the diff.

Or maybe the avatars just need to be built for unity in a way. Hang tight here…

OK. Unity Standard Assets… the Ethan Avatar worked for me. Try that.

Where is the avatar for Ethan in standard assest folder?

Just search Ethan, or even click on the picker for your current avatar for your character, and you’ll see it listed. If you don’t, then you might need to re-download the standard assets.

In the animator in inspector i Changed it to ethan avatar and it didnt work, still cant look up and down

So does anyone know why this is happening?