- Home /
Question by
jonathanbooker2911 · Jan 21, 2019 at 07:07 AM ·
scripting problemscripting beginner
Camera issues.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CameraController : MonoBehaviour {
public float x;
public float y;
public float rotationspeed;
void Update()
{
Rotation();
}
public void Rotation()
{
x = Input.GetAxis("Mouse X") * rotationspeed;
y = Input.GetAxis("Mouse Y") * rotationspeed;
transform.eulerAngles = new Vector3(y, x, 0);
}
}
My camera keeps resetting back to the center of the screen everytime I move along the y or x mouse axis. I'm not sure why.
Comment
Best Answer
Answer by Hellium · Jan 21, 2019 at 07:48 AM
@artofshashank is right, but I believe what you are looking for is an FPS-like camera. So you will have to do the following :
public void Rotation()
{
x += Input.GetAxis("Mouse X") * rotationspeed; // Mind the += operator
y += Input.GetAxis("Mouse Y") * rotationspeed; // Mind the += operator
transform.eulerAngles = new Vector3(y, x, 0);
}
Answer by artofshashank · Jan 21, 2019 at 07:28 AM
This because as soon as you stop moving your mouse, the GetAxis of both, x and y start returning 0, but the Update is still executing; resulting in variables x and y getting 0, and thus your problem. Try keeping this rotation on a mouse button press or something may be? Like:
void Update()
{
if(!Input.MouseButton(0))
return;
Rotation();
}