I want to make a fps game. To create first person viewer I followed this tutorial youtube
I followed everything that he told to do and I could successfully look up or down but could not look sideways.
Here`s the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mouselook : MonoBehaviour
{
float mousesenitivevity = 100f;
public Transform player_eyes;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mousesenitivevity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mousesenitivevity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
player_eyes.Rotate(Vector3.up * mouseX);
}
}
I really do not know where the error is occurring but here are a few things I noticed:
If you comment out the line transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f); then the player can look sideways but not up and down and if you uncomment it the player can look up and down but not sideways.
Here is the unity editor screenshots for more details:
Editor image
Solved, I put the script on the player instead of the camera by mistake.
Related
I was trying trying to get my right joystick to control my first person camera in unity. I've tried duplicating Mouse X and Mouse Y but it always snaps upwards. How do I get this working?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
private float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// 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;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
Ive been trying to add recoil to my fps game (Used Brackeys tutorials for the base), but the camera looking script is causing the rotation to be set each frame. I cant figure out how to change the camera rotation without the script resetting it
Code for the looking script is attatched below
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 1500f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// 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;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
To change the camera rotation without the script resetting it I would use an if statement to check if a different rotation should be applied ex:
bool canLook = true;
if(canLook == false) {
//apply your desired rotation
}
else
{
//run your looking code
}
//When you want to apply your rotation set canLook to false
//And when you want to return to the camera movement set canLook to true
So i am really really new to Unity and C# and i was just trying out some things. Now i want to kinda polish the overall control experience. So i want the Camera to rotate 5° on the Z axis when the Button A is pressed.
Thats what i tried:
if (Input.GetKeyDown(KeyCode.A))
{
if (zRotation < 5f)
{
transform.localRotation = Quaternion.Euler(zRotation, 5f, 5f);
}
}
It somewhat works, the camera rotates to 5° on the Z but only for i split second and then returns to it's normal state.
Full Code of the Camera Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
float zRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// 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;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
if (Input.GetKeyDown(KeyCode.A))
{
if (zRotation < 5f)
{
transform.localRotation = Quaternion.Euler(zRotation, 5f, 5f);
}
}
}
}
I want the Camera to rotate 5° on the Z axis when the Button A is pressed.
You don't need any variables to achieve this, it should be as simple as:
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
cameraTransform.Rotate(0, 0, 5);
}
}
I am following the Brackeys tutorial for 3d movement and camera, and I keep getting the error:
The type or namespace name transform could not be found
I've tried changing it and I'm extremely new to coding so I don't really know what to do.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mouselook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// 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;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
I think you are just missing a capital on transform.
public Transform playerBody;
In Unity I used this little script to move the mouse around like in an FPS, but looking from side to side is extremely buggy and it will only move a bit in either direction before defaulting to looking straight forward. Looking up and down works perfectly, and I have a clamping script to only look up 90 degrees or down 90 degrees. For some reason, before adding in the code for looking up and down, it worked perfectly, but the two together makes it all buggy. Any help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSpeed = 100f;
public Transform playerBody;
float xRotation = 0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSpeed * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSpeed * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
you may have to experiment but this line looks like it doesnt belong
xRotation -= mouseY;
This is a problem i get too when i first make fps fames and i think i fix it by adding the code to FixedUpdate(). Alternativelly you can try and switching the playerBody.Rotate() with the transform.localRotation