- Home /
making a camera that moves depending on mousepos.y
So im trying to make it so when the mouse moves down, the camera moves from above the player looking down, to backwards, downwards, and still looking at the player.
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public int Sensitivity;
private float MouseY;
private float MouseYSet;
private float CameraAngle;
public GameObject CameraTarget;
void Start () {
}
void Update () {
// Rotation controls
MouseY += Input.GetAxisRaw ("Mouse Y");
transform.localRotation = Quaternion.Euler (Mathf.Clamp (transform.localRotation.x, -90, 90), transform.localRotation.y, transform.localRotation.z);
transform.localPosition = new Vector3 (transform.localPosition.x, Mathf.Clamp (transform.localPosition.y, CameraTarget.transform.localPosition.y, CameraTarget.transform.localPosition.y + 50), Mathf.Clamp (transform.localPosition.z, CameraTarget.transform.localPosition.z, CameraTarget.transform.localPosition.z + 50));
CameraAngle = Vector3.Angle (transform.position, CameraTarget.transform.position);
if (MouseYSet != MouseY)
{
transform.localRotation = Quaternion.Slerp (Quaternion.Euler (transform.localRotation.x, transform.localRotation.y, transform.localRotation.z), Quaternion.Euler (transform.localRotation.x - (MouseY - MouseYSet), transform.localRotation.y, transform.localRotation.z), (MouseY - MouseYSet) * Sensitivity * 10 * Time.deltaTime);
transform.localPosition = Vector3.Slerp (new Vector3 (transform.localPosition.x, transform.localPosition.y, transform.localPosition.z), new Vector3 (CameraTarget.transform.position.x, transform.position.y - (MouseY - MouseYSet), CameraTarget.transform.position.z), (MouseY - MouseYSet) * Sensitivity * 10 * Time.deltaTime);
MouseYSet = MouseY;
}
}
}
Explain what your problem is, what exactly is not working?
Is your code not doing anything at all or is it working a bit?
$$anonymous$$y initial thought to this would be to use ".LookAt(" to keep the focus on the player, and to use ".RotateAround(" with the mouse axis.
It's shaking how exactly? Does it shake side-to-side or just when you move it up and down?
Besides the shaking everything works as it should?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Angles from quaternion/vector Problem. 2 Answers
how to convert the angle to roatation? 1 Answer
Rotate vector around vector? 2 Answers
Raycasting at an offset angle? 4 Answers