Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by Yettie123, Jun 30, 2017.

  1. Yettie123

    Yettie123

    Joined:
    Jun 20, 2017
    Posts:
    79
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class camMouseLook : MonoBehaviour
    6. {
    7.  
    8.     public Transform characterTransform;
    9.     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    10.     public RotationAxes axes = RotationAxes.MouseXAndY;
    11.     public float sensitivityX = 15F;
    12.     public float sensitivityY = 15F;
    13.     public float minimumX = -360F;
    14.     public float maximumX = 360F;
    15.     public float minimumY = -60F;
    16.     public float maximumY = 60F;
    17.     float rotationY = 0F;
    18.  
    19.     void Update()
    20.     {
    21.         if (axes == RotationAxes.MouseXAndY)
    22.         {
    23.             float rotationX = characterTransform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
    24.  
    25.             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    26.             rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
    27.  
    28.             characterTransform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
    29.         }
    30.         else if (axes == RotationAxes.MouseX)
    31.         {
    32.             characterTransform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
    33.         }
    34.         else
    35.         {
    36.             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    37.             rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
    38.  
    39.             characterTransform.localEulerAngles = new Vector3(-rotationY, characterTransform.localEulerAngles.y, 0);
    40.         }
    41.     }
    42.  
    43.  
    44. }
    Character Control Code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterControl : MonoBehaviour
    6. {
    7.  
    8.     private float speed = 0;
    9.     public float wSpeed;
    10.     public float rSpeed;
    11.  
    12.     static Animator anim;
    13.  
    14.     void Start()
    15.     {
    16.         Cursor.lockState = CursorLockMode.Locked;
    17.  
    18.         anim = GetComponent<Animator>();
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         float z = Input.GetAxis("Vertical") * speed;
    24.         float y = Input.GetAxis("Horizontal") * speed;
    25.  
    26.         transform.Translate(0, 0, z);
    27.         transform.Translate(y, 0, 0);
    28.  
    29.         if (Input.GetKeyDown("escape"))
    30.             Cursor.lockState = CursorLockMode.None;
    31.  
    32.         if (Input.GetKey(KeyCode.W))
    33.         {
    34.             speed = rSpeed;
    35.             anim.SetBool("isWalk", false);
    36.             anim.SetBool("isIdle", false);
    37.             anim.SetBool("isRun", true);
    38.         }
    39.         if (Input.GetKey("left shift") && Input.GetKey(KeyCode.W))
    40.         {
    41.             speed = rSpeed;
    42.             anim.SetBool("isWalk", false);
    43.             anim.SetBool("isIdle", false);
    44.             anim.SetBool("isRun", true);
    45.         }
    46.         else if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.A) && !Input.GetKeyUp("left shift"))
    47.         {
    48.             speed = wSpeed;
    49.             anim.SetBool("isWalk", true);
    50.             anim.SetBool("isIdle", false);
    51.             anim.SetBool("isRun", false);
    52.         }
    53.         //Idle
    54.         else
    55.         {
    56.             anim.SetBool("isWalk", false);
    57.             anim.SetBool("isIdle", true);
    58.             anim.SetBool("isRun", false);
    59.         }
    60.     }
    61.  
    62. }
    Thanks for any help!
     
  2. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    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.

    Code (csharp):
    1. float z = Input.GetAxis("Vertical") * speed;
    2. float y = Input.GetAxis("Horizontal") * speed;
    3.  
    4. transform.Translate(0, 0, z);
    5. 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!
     
    Last edited: Jun 30, 2017
    idurvesh likes this.
  3. Yettie123

    Yettie123

    Joined:
    Jun 20, 2017
    Posts:
    79
    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.
     
  4. Yettie123

    Yettie123

    Joined:
    Jun 20, 2017
    Posts:
    79
    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.
     
  5. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    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...
     
  6. Yettie123

    Yettie123

    Joined:
    Jun 20, 2017
    Posts:
    79
    Yea I just temporarily disabled the animator and it worked, weird. What did you change the avatar too?
     
  7. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    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..
     
  8. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
  9. Yettie123

    Yettie123

    Joined:
    Jun 20, 2017
    Posts:
    79
    Where is the avatar for Ethan in standard assest folder?
     
  10. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    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.
     
  11. Yettie123

    Yettie123

    Joined:
    Jun 20, 2017
    Posts:
    79
    In the animator in inspector i Changed it to ethan avatar and it didnt work, still cant look up and down
     
  12. Yettie123

    Yettie123

    Joined:
    Jun 20, 2017
    Posts:
    79
    So does anyone know why this is happening?