- Home /
I need some help with camera rotations
Well, I am trying to make an effect on my 3d top-down shooter game. This effect includes the camera's rotation. Basically, what I want to do, is to keep the camera at the player, but when I move the mouse on the screen (lets just say I move it top right), the camera should extend towards that point, but never actually look away from the player.
This works, as long as I only use either the X or Y axis. If I use them both, the entire camera freaks out and starts swinging wide everywhere.
My code this far (It's on the camera):
using UnityEngine;
using System.Collections;
public class PlayerCamera : MonoBehaviour {
public Vector3 offset = new Vector3(0,0,0);
public float cameraReach = 16;
float xRotation = 0.0f;
float yRotation = 0.0f;
GameObject player;
void Start ()
{
player = GameObject.Find("Plr_Player");
}
void Update ()
{
//xRotation = ((Input.mousePosition.x - (Screen.height / 2)) / cameraReach);
//yRotation = ((Input.mousePosition.y - (Screen.height / 2)) / cameraReach);
xRotation = (-transform.rotation.eulerAngles.x + (((Input.mousePosition.x - (Screen.height / 2)) / cameraReach)));
yRotation = (-transform.rotation.eulerAngles.y - (((Input.mousePosition.y - (Screen.height / 2)) / cameraReach)));
}
void LateUpdate ()
{
transform.position = player.transform.position + offset;
transform.rotation = Quaternion.Euler(yRotation, xRotation , transform.rotation.eulerAngles.z);
}
}
Comment
Try replacing
xRotation = (-transform.rotation.eulerAngles.x + (((Input.mousePosition.x - (Screen.height / 2)) / cameraReach)));
yRotation = (-transform.rotation.eulerAngles.y - (((Input.mousePosition.y - (Screen.height / 2)) / cameraReach)));
with
xRotation = (-xRotation + (((Input.mousePosition.x - (Screen.height / 2)) / cameraReach)));
yRotation = (-yRotation - (((Input.mousePosition.y - (Screen.height / 2)) / cameraReach)));