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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED] C# Character Rotation relative to camera's (First Person)

Discussion in 'Scripting' started by Raminul, Oct 11, 2015.

  1. Raminul

    Raminul

    Joined:
    Oct 11, 2015
    Posts:
    8
    Hello, I've been at this for a few hours now. And I've partially been able to achieve what I wanted, although not quite.
    I've been searching for answers for a while, and although there are very similar questions going around, no answers that've been provided have actually helped me.

    So, basically. I've been able to make it so the character I have in the scene, is facing the same way the camera is, kind of. When I rotate the camera, the character follows, but it lags behind, and eventually, the controls in terms of directions will be all off.

    I've been at this for a few hours, with no luck.

    Any help would be appreciated. Here are my two scripts prepared for you.

    Camera Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class cameraScript : MonoBehaviour
    4. {
    5.         public float MouseSensitivity = 1.0f;
    6.      
    7.         public float XRotation;
    8.         public float YRotation;
    9.         float MinYRotation = -90f;
    10.         float MaxYRotation = 90f;
    11.         // Use this for initialization
    12.         void Start ()
    13.         {
    14.                 InitCamera ();
    15.         }
    16.      
    17.         // Update is called once per frame
    18.         void Update ()
    19.         {
    20.                 CameraController();
    21.         }
    22.          void InitCamera()
    23.         {
    24.                 //Finding the Player's transform and assigning the camera's position to the Player's.
    25.                 GameObject myTarget = GameObject.FindGameObjectWithTag("Player");
    26.                 Vector3 DesiredPos = new Vector3(myTarget.transform.position.x, transform.position.y, myTarget.transform.position.z);
    27.                 transform.position = DesiredPos;
    28.                 transform.parent = myTarget.transform;
    29.         }
    30.         void CameraController()
    31.         {
    32.                 //Assigning rotations to appopriate Axes
    33.                 XRotation += Input.GetAxisRaw ("Mouse X") * MouseSensitivity;
    34.                 YRotation -= Input.GetAxisRaw ("Mouse Y") * MouseSensitivity;
    35.                 //Clamp
    36.                 YRotation = Mathf.Clamp (YRotation,MinYRotation, MaxYRotation );
    37.                 //Actual Rotation
    38.                 transform.localRotation = Quaternion.Euler (YRotation,XRotation,0);
    39.         }
    40. }

    Controller Script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class PlayerController : MonoBehaviour
    4. {
    5.         public float MovementSpeed = 5.0f;
    6.         // Use this for initialization
    7.         void Start ()
    8.         {
    9.      
    10.         }
    11.      
    12.         // Update is called once per frame
    13.         void Update ()
    14.         {
    15.                 float localRotationY = Camera.main.transform.rotation.y;
    16.                 float MoveHorizontal = Input.GetAxis ("Horizontal") * Time.deltaTime * MovementSpeed;
    17.                 float   MoveVertical = Input.GetAxis("Vertical") * Time.deltaTime * MovementSpeed;
    18.                 transform.Translate (MoveHorizontal, 0 , MoveVertical);
    19.                 transform.localRotation = Quaternion.Euler (0, Camera.main.transform.localEulerAngles.y, 0);
    20.         }
    21. }

     
    Last edited: Oct 12, 2015
  2. Raminul

    Raminul

    Joined:
    Oct 11, 2015
    Posts:
    8
    Updated post so you wouldn't need to view the code by using pastebin.
     
  3. Raminul

    Raminul

    Joined:
    Oct 11, 2015
    Posts:
    8
    I fixed it! :D I finally figured it out!

    Instead of using localrotation I'd use the actual Eulerangles themselves..

    transform.eulerAngles = new Vector3(YRotation,XRotation,0);

    instead of

    transform.localRotation=Quaternion.Euler(YRotation,XRotation,0);