How do I make my third person shooter player look around? - c#

I've genuinely gone through a million tutorials, but it doesn't work out. With this code, I either have to activate the last line or the second last line to get x rotation or y rotation respectively, but I obviously want them to work together.
void MouseLook()
{
mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -60, 10);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
transform.Rotate(Vector3.up * mouseX);
}
The one always seems to cancel the other out. This is from a Brackeys tutorial. I had to tweak some stuff so it's applicable, but I obviously am breaking the code that way. Please help!

Try this Stuff
public float mouseSensitivity = 10.0f;
public Transform target;
public float dstFromTarget = 2.0f;
public float yaw;
public float pitch;
public Vector2 pitchMinMax = new Vector2(-50, 85);
public float rotationSmoothTime = 0.02f;
Vector3 rotationSmoothVelocity;
Vector3 currentRotation;
void LateUpdate()
{
MouseLook();
}
void MouseLook()
{
//Mouse Movement
yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
// Claemp
pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y);
// Positioning
transform.eulerAngles = currentRotation;
// Smoothening
currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
transform.localRotation = Quaternion.Euler(currentRotation.x,currentRotation.y,currentRotation.z);
}

Related

How do I fix my camera's xRotation's Clamping?

I made a first person camera using parts of Brackeys Guide, I have camera moving on both the axis but the line for the xRotation clamp doesn't work when I run the game. How can I fix this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 200f;
public Transform playerBody;
public Transform cam;
private float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked; // stops you from click out off the sceen
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime; // mouse imput
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime; // mouse imput
xRotation = mouseY;
xRotation = Mathf.Clamp(xRotation, -80f, 73f); // stops you from breaking your neck
Quaternion localRotation = Quaternion.Euler(xRotation, 0f, 0f); // left and right movement
playerBody.Rotate(Vector3.up * mouseX); // left and right movement
cam.Rotate(Vector3.left * mouseY); // up and down movement
}
Setting new specific variables like roty and rotx, and clamping them individually.
It may be that the Vector3 vector has no value, if a value is generated, it can be
private Vector3 m_Dir
void Update () {
this.m_Dir = new Vector3 (localRotation, Vector3.up * mouseX,
tVector3.left * mouseY);
playerBody.Rotate(m_Dir);
cam.Rotate(m_Dir);
}
There are two main problems in the Update method:
xRotation = mouseY is not correct, since mouseY represents the delta angle you want to rotate in this frame, while xRotation represents the current angle at which the camera is rotated. You have to do a sum to get the next pitch of the camera, so xRotation += mouseY.
The quaternion localRotation is created but not actually used in the rotating logic, you still rotate the camera with cam.Rotate(Vector3.left * mouseY). Replace it with cam.localRotation = localRotation.
The final code should look like this:
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
// note: you shouldn't hardcode the min and max values,
// use some values that you can change in the inspector
xRotation = Mathf.Clamp(xRotation + mouseY, -80f, 73f);
cam.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);

Camera Rotation based on Keydown

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);
}
}

Inversion of mouse in Unity

I have this code:
using UnityEngine;
public class MouseLook : MonoBehaviour
{
float speed = 120f;
public Transform playerBody;
float xRotation = 200f;
float yRotation = 200f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * speed * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * speed * Time.deltaTime;
playerBody.Rotate(Vector3.left * mouseY);
playerBody.Rotate(Vector3.up * mouseX);
xRotation -= mouseX;
yRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -25f, 30f);
yRotation = Mathf.Clamp(yRotation, -35f, 40f);
transform.localRotation = Quaternion.Euler(yRotation, xRotation, 0f);
}
}
The only problem is that when using it, the mouse is inverted on the X-axis. That is, when the mouse moves to the right, the camera moves to the left and vice versa. There is no such inversion with the Y-axis.
What wrong with this code?
When rotating an object around X axis, positive angles rotate down and negative angles rotate up. When rotating an object around Y axis, positive angles rotate right and negative angles rotate left.
Screen coordinates in Unity go from bottom left (0,0) to top right (x,y). When you move your mouse upward you get positive y values, when you move your mouse downward you get negative y values. Notice, that this is inverse of object rotation. That's why this line: yRotation -= mouseY; works fine (it inverts y values before rotating the object). However, for x values inversion isn't necessary since Screen space and Object space already align. You can just use: xRotation += mouseX; instead.
I don't know exactly what you are trying to achieve, but this looks weird to me: xRotation = Mathf.Clamp(xRotation, -25f, 30f);, because it clamps rotation to the left to 25 degrees and to the right to 30 degrees. Is this on purpose?
Try this code:
using UnityEngine;
public class MouseLook : MonoBehaviour
{
float speed = 120f;
public Transform playerBody;
float xRotation = 0f;
float yRotation = 0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * speed * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * speed * Time.deltaTime;
playerBody.Rotate(Vector3.left * mouseY);
playerBody.Rotate(Vector3.up * mouseX);
xRotation += mouseX;
yRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -25f, 30f);
yRotation = Mathf.Clamp(yRotation, -35f, 40f);
transform.localRotation = Quaternion.Euler(yRotation, xRotation, 0f);
}
}

Unity camera flicks across screen, first person

Hello I am new to UNITY I have written some code for a first person camera however sometimes when I am looking around it will flick what way the camera is facing. This is my code
public float mouseSensitivity = 200f;
public Transform playerBody;
float yRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// 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;
yRotation -= mouseY;
yRotation = Mathf.Clamp(yRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(yRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
I have a video to show my issue as well https://youtu.be/nMopJzNyYr4 for is the question is not clear.
I have a capsule as my parent it is the players actual body and then my camera is a child to the playerBody ( my capsule )
Try it without the deltaTime multiplication. GetAxis on a mouse axis gives you a difference in mouse position, which should not be multiplied by deltaTime or any other time delta unless you are interested in a measurement of Absement which you probably are not!!
You are more interested in a distance measurement, so you can just use
float mouseABC = Input.GetAxis("Mouse ABC") * mouseSensitivity;.
Alternatively, if you were to want the speed of the mouse movement, you would actually rather divide by time.deltaTime such as float mouseHorizontalVelocity = Input.GetAxis("Mouse X") * mouseSensitivity / Time.deltaTime;.

unity rotate object on x-axis when moving mouse up / down

I want to rotate a object on the x-axis when I move the mouse up or down (increase the x-rotation when moving mouse up, decrease when moving mouse down).
But I don't know how to do this.
I tried this script:
public float mouseSensitivity = 100.0F;
public float clampAngle = 80.0F;
float rotX = 0.0F, rotY = 0.0F;
void Update()
{
// Mouse Look
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
rotX += mouseY * mouseSensitivity * Time.deltaTime;
rotY += mouseX * mouseSensitivity * Time.deltaTime;
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler(rotX, 0, 0.0F);
transform.rotation = localRotation;
}
But this controls also rotation on the y axis. I also have rotation on the y-axis, but they are controlled via keyboard input:
if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S))
transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
else if (Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.W))
transform.Translate(Vector3.forward * Time.deltaTime * -moveSpeed);
if (Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D))
transform.Rotate(Vector3.up * Time.deltaTime * -rotateSpeed, Space.World);
else if (Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A))
transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed, Space.World);
and I want the mouse up/down only control the rotation on the x-axis.
If someone can help me, that would be great!
void Update()
{
// Mouse Look
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
rotX = mouseY * mouseSensitivity;
rotY = mouseX * mouseSensitivity;
transform.rotation *= Quaternion.Euler(rotX, 0, 0.0f);
}

Categories

Resources