Hello Community,
first of all:
i look for an simple answer, it does not need to be perfect, but it should be simple and easy to understand for somebody new like me
Now here is my question:
Im making a 3D space game, and before i do anything i need my players to be able to look around.
Because this is in space and the players have jetpacks, the players body should rotate with EVERY mouse movement, that means it rotates around x and y at the same time.
Here is my problem:
I know how to make it so the camera turns around x / y axis but not both, and i need a script that is simple and handles both in one camera.
I currently use transform.Rotate, this works fine for y and x axis on their own, but when i do both together, and move the camera around x and y at the same time (ingame) it will start rotating weirdly.
Thanks in advance and again i need:
-simple code
-in one script bound to the cam
-that moves in every direction without weird tilting
2 Likes
Thank you for your answer, but it does not seem to work for me. Also its difficult to understand because there is no comment at all in this script so i dont know whats going on
1 Like
I put in some comments to help you understand it better
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[AddComponentMenu("Camera-Control/Smooth Mouse Look")]
public class SmoothMouseLook : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationX = 0F;
float rotationY = 0F;
private List<float> rotArrayX = new List<float>();
float rotAverageX = 0F;
private List<float> rotArrayY = new List<float>();
float rotAverageY = 0F;
public float frameCounter = 20;
Quaternion originalRotation;
void Update ()
{
if (axes == RotationAxes.MouseXAndY)
{
//Resets the average rotation
rotAverageY = 0f;
rotAverageX = 0f;
//Gets rotational input from the mouse
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
//Adds the rotation values to their relative array
rotArrayY.Add(rotationY);
rotArrayX.Add(rotationX);
//If the arrays length is bigger or equal to the value of frameCounter remove the first value in the array
if (rotArrayY.Count >= frameCounter) {
rotArrayY.RemoveAt(0);
}
if (rotArrayX.Count >= frameCounter) {
rotArrayX.RemoveAt(0);
}
//Adding up all the rotational input values from each array
for(int j = 0; j < rotArrayY.Count; j++) {
rotAverageY += rotArrayY[j];
}
for(int i = 0; i < rotArrayX.Count; i++) {
rotAverageX += rotArrayX[i];
}
//Standard maths to find the average
rotAverageY /= rotArrayY.Count;
rotAverageX /= rotArrayX.Count;
//Clamp the rotation average to be within a specific value range
rotAverageY = ClampAngle (rotAverageY, minimumY, maximumY);
rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX);
//Get the rotation you will be at next as a Quaternion
Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
//Rotate
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
else if (axes == RotationAxes.MouseX)
{
rotAverageX = 0f;
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotArrayX.Add(rotationX);
if (rotArrayX.Count >= frameCounter) {
rotArrayX.RemoveAt(0);
}
for(int i = 0; i < rotArrayX.Count; i++) {
rotAverageX += rotArrayX[i];
}
rotAverageX /= rotArrayX.Count;
rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX);
Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else
{
rotAverageY = 0f;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotArrayY.Add(rotationY);
if (rotArrayY.Count >= frameCounter) {
rotArrayY.RemoveAt(0);
}
for(int j = 0; j < rotArrayY.Count; j++) {
rotAverageY += rotArrayY[j];
}
rotAverageY /= rotArrayY.Count;
rotAverageY = ClampAngle (rotAverageY, minimumY, maximumY);
Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
transform.localRotation = originalRotation * yQuaternion;
}
}
void Start ()
{
Rigidbody rb = GetComponent<Rigidbody>();
if (rb)
rb.freezeRotation = true;
originalRotation = transform.localRotation;
}
public static float ClampAngle (float angle, float min, float max)
{
angle = angle % 360;
if ((angle >= -360F) && (angle <= 360F)) {
if (angle < -360F) {
angle += 360F;
}
if (angle > 360F) {
angle -= 360F;
}
}
return Mathf.Clamp (angle, min, max);
}
13 Likes
AndrewTheLeet:
I put in some comments to help you understand it better
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[AddComponentMenu("Camera-Control/Smooth Mouse Look")]
public class SmoothMouseLook : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationX = 0F;
float rotationY = 0F;
private List<float> rotArrayX = new List<float>();
float rotAverageX = 0F;
private List<float> rotArrayY = new List<float>();
float rotAverageY = 0F;
public float frameCounter = 20;
Quaternion originalRotation;
void Update ()
{
if (axes == RotationAxes.MouseXAndY)
{
//Resets the average rotation
rotAverageY = 0f;
rotAverageX = 0f;
//Gets rotational input from the mouse
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
//Adds the rotation values to their relative array
rotArrayY.Add(rotationY);
rotArrayX.Add(rotationX);
//If the arrays length is bigger or equal to the value of frameCounter remove the first value in the array
if (rotArrayY.Count >= frameCounter) {
rotArrayY.RemoveAt(0);
}
if (rotArrayX.Count >= frameCounter) {
rotArrayX.RemoveAt(0);
}
//Adding up all the rotational input values from each array
for(int j = 0; j < rotArrayY.Count; j++) {
rotAverageY += rotArrayY[j];
}
for(int i = 0; i < rotArrayX.Count; i++) {
rotAverageX += rotArrayX[i];
}
//Standard maths to find the average
rotAverageY /= rotArrayY.Count;
rotAverageX /= rotArrayX.Count;
//Clamp the rotation average to be within a specific value range
rotAverageY = ClampAngle (rotAverageY, minimumY, maximumY);
rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX);
//Get the rotation you will be at next as a Quaternion
Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
//Rotate
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
else if (axes == RotationAxes.MouseX)
{
rotAverageX = 0f;
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotArrayX.Add(rotationX);
if (rotArrayX.Count >= frameCounter) {
rotArrayX.RemoveAt(0);
}
for(int i = 0; i < rotArrayX.Count; i++) {
rotAverageX += rotArrayX[i];
}
rotAverageX /= rotArrayX.Count;
rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX);
Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else
{
rotAverageY = 0f;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotArrayY.Add(rotationY);
if (rotArrayY.Count >= frameCounter) {
rotArrayY.RemoveAt(0);
}
for(int j = 0; j < rotArrayY.Count; j++) {
rotAverageY += rotArrayY[j];
}
rotAverageY /= rotArrayY.Count;
rotAverageY = ClampAngle (rotAverageY, minimumY, maximumY);
Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
transform.localRotation = originalRotation * yQuaternion;
}
}
void Start ()
{
Rigidbody rb = GetComponent<Rigidbody>();
if (rb)
rb.freezeRotation = true;
originalRotation = transform.localRotation;
}
public static float ClampAngle (float angle, float min, float max)
{
angle = angle % 360;
if ((angle >= -360F) && (angle <= 360F)) {
if (angle < -360F) {
angle += 360F;
}
if (angle > 360F) {
angle -= 360F;
}
}
return Mathf.Clamp (angle, min, max);
}
First of all: sorry for the slow response and thank you for your comments, but i managed to handle the camera pretty well on my own. but thank you for your work
I know I’m super late, but the When using Input from a specific axis it is good to multiply it from Time.deltaTime and calibrate it. I experienced Ultra high sensitivity in my PC (I’ve turned down VSync on the editor so it goes like 2000 fps) and in my mobile it is barely impossible to move the camera.
Why you people working so hard. It is already done in standard assets. Drag first person camera to the view and run your game. So simple
1 Like
Runley
January 17, 2019, 9:35pm
10
cuz people like to make their own scripts, its more rewarding than just drag and drop
10 Likes
Ok idk if anybody still on here but what do i call the cameras script? I named it camera, SmoothMouseLook and still
have errors. Please help me
Snuwiu
February 12, 2020, 2:09pm
14
Maby try to call it CameraRotation or something like that. this should work
You have to name it “SmoothMouseLook”
Like At the top of the Skript
Prella
April 5, 2020, 11:44pm
17
i dont care abput you long script
1 Like
Prella
April 5, 2020, 11:45pm
18
Please post something simple
Lmao you aren’t going to learn anything with that attitude. If the idea of a long script is too daunting then maybe programming isn’t for you.
5 Likes
Ok but i have seen different scripts but smaller. I am new to c# but there might be some unnecessary parts. For example: he made a float for the clamping but he could have just wrote the number 90 and -90