Search Unity

Help with X axis Character rotation

Discussion in 'Scripting' started by L32, Aug 30, 2015.

  1. L32

    L32

    Joined:
    Aug 30, 2015
    Posts:
    3
    Hello everybody,

    I seem to be having trouble with my characters movement rotating on the X axis.
    Basically the problem is that it works perfectly on another computer, however when I use the script on this project it doesn't seem to work and I cannot, for the life of me work out what I am missing.
    The Yaxis movement works as intended, the character movement/jumping works as intended however the camera nor the player gameobject itself rotate on the Xaxis.

    This is my Character controller script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CharacterController2 : MonoBehaviour
    5. {
    6.     public float speed = 6.0F;
    7.     public float jumpSpeed = 8.0F;
    8.     public float gravity = 20.0F;
    9.     private Vector3 moveDirection = Vector3.zero;
    10.     void Update()
    11.     {
    12.         CharacterController controller = GetComponent<CharacterController>();
    13.         if (controller.isGrounded)
    14.         {
    15.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    16.             moveDirection = transform.TransformDirection(moveDirection);
    17.             moveDirection *= speed;
    18.             if (Input.GetButton("Jump"))
    19.                 moveDirection.y = jumpSpeed;
    20.        
    21.         }
    22.         moveDirection.y -= gravity * Time.deltaTime;
    23.         controller.Move(moveDirection * Time.deltaTime);
    24.     }
    25. }
    and this is my Cameracontroller Script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraController : MonoBehaviour
    5. {
    6.     public GameObject camera;
    7.     float cameraRotX = 0f;
    8.     public float cameraPitchMax = 70f;
    9.  
    10.     // Update is called once per frame
    11.     void Update ()
    12.     {
    13.         cameraRotX -= Input.GetAxis ("Mouse Y") * 5f;
    14.    
    15.         cameraRotX = Mathf.Clamp (cameraRotX, -cameraPitchMax, cameraPitchMax);
    16.  
    17.         Quaternion rot = Quaternion.Euler (cameraRotX, 0, 0);
    18.  
    19.         transform.localRotation = rot;
    20.     }
    21. }
    22.  
    Also if this is any use here is my game object hierarchy in the editor: Playerhirachy.PNG

    And here's the Camera Component:
    CameraInspector.PNG

    Any help would be much appreciated, thanks in advance.

    edit: removed image links and manually uploaded
     
    Last edited: Aug 30, 2015
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    What but doesn't work on the new PC? Does it move but not the correct way or does it just not move at all? Any console messages?
     
  3. L32

    L32

    Joined:
    Aug 30, 2015
    Posts:
    3
    Everything works appart from the Xaxis horizontal rotation (Mouse movement) on the character, ie. he cant look left or right, but can look up and down when using the mouse.
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I don't have my PC handy & it's late here so this may be wrong, but if the character is looking left/right shouldn't the rotation be around the Y axis?
     
  5. L32

    L32

    Joined:
    Aug 30, 2015
    Posts:
    3
    Yeah this is the only error I seem to get:
    Assets/Scripts#/CameraController.cs(6,27): warning CS0108: `CameraController.camera' hides inherited member `UnityEngine.Component.camera'. Use the new keyword if hiding was intended
     
  6. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    In your script you are setting the y rotation to 0 & the x rotation to the value you want. I think it should be the other way around