Home > Mobile >  Why can't I look horizontally in Unity3D after adding this line(FPS)?
Why can't I look horizontally in Unity3D after adding this line(FPS)?

Time:10-02

I was following Brackeys's tutorial on youtube to make my character look horizontally and vertically, at first I was able to look to my right and left in with my mouse, but after I added the line near the end of void update()

[transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);]

I can look up and down but can't look to my left and right anymore. Could anyone tell me how to fix it? It is confusing because it worked fine in the video tutorial.

here's my code:

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

public class MouseLook : MonoBehaviour
//https://www.youtube.com/watch?v=_QajrabyTJc
{
public float MouseSensitivity = 100f;
//this variable controls the speed of the mouse
public Transform PlayerBody;
float xRotation = 0f;


// Start is called before the first frame update
void Start()
{
    Cursor.lockState = CursorLockMode.Locked;
    //this line is to hide the ouse cursor in game
}

// Update is called once per frame
void Update()
{
    float mouseX = Input.GetAxis("Mouse X") * MouseSensitivity * Time.deltaTime;
    float mouseY = Input.GetAxis("Mouse Y") * MouseSensitivity * Time.deltaTime;
    //these 2 variables will store the movement input from mouse

    //this enables us to look horizontally (rotate around Y Axis)
    //InvalidOperationException: You are trying to read Input using the UnityEngine.Input class, but you have switched active Input handling to Input System package in Player Settings.
    //MouseLook.Update()  this is a but that will appear after this line was added, to fix this 
    //Project Settings > Player > Active Input Handling change it to both and add camera to playerbody
    xRotation -= mouseY;
    xRotation = Mathf.Clamp(xRotation,-90,90f);//this line prevent player to rotate over its head/crotch

    transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f); *This line!*
    PlayerBody.Rotate(Vector3.up * mouseX);
    //line 31-33 is for the function to be able to look up and down
    //remember Quaternion is about rotation

}

}

CodePudding user response:

Thank you guys! It worked after I:

  1. Declared this new variable
float yRotation = 0f;
  1. Set yRotation to be equal to mouseX itself
yRotation  = mouseX;
  1. Changed the problematic line to what @HiggHigg suggested
transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f);

Now I can look to 4 directions in FPS with my cursor, the screen kinda shivers when I move though, maybe I'll find a way to fix it later.

CodePudding user response:

This line:

transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);

It in the Update void, so it will call every frame. You can't look vertically because every in every frame, this line will make your character y rotation equal to 0

You can see more in here

To fix that you can make a variable call yRotation or something you want then make it like the xRotation but with your mouseY variable and then edit that line to

transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f);
  • Related