Help With Camera rotation on X and Y Axis (Target Focused)
Hey so im trying to develop a camera that works similar to PotBS (Pirates of the Burning Sea) or ETS/ATS (Truck Simulators). So far I have it so the camera can zoom in and out and rotate around the object no problem. I am having trouble however when I add another axis. I tried adding a separate game object within the object im looking at so that I could assign the two separate axis to look at a different object but not sure if it helped.
So when I move the mouse left or right, its works great and I can rotate around no probs, but the Y axis is bugging out! I would alos like to implement so limits as to how 'High' the camera can go and also stop it from going below. Heres what I have so far
using UnityEngine;
using System.Collections;
public class PlayerCamera : MonoBehaviour {
public Transform target = null;
public Transform target2 = null;
int degrees = 10;
private Vector3 tempPos;
private Vector3 tempRot;
// Zoom settings
private float minFov = 15f;
private float maxFov = 90f;
private float sensitivity = 30f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void LateUpdate ()
{
if (target != null) {
transform.LookAt (target.transform);
}
if (Input.GetMouseButton (2)) {
transform.RotateAround (target.position, Vector3.up, Input.GetAxis ("Mouse X") * degrees);
transform.RotateAround (target2.position, -Vector3.right, Input.GetAxis ("Mouse Y") * degrees);
tempPos = transform.position;
tempRot = transform.rotation.eulerAngles;
transform.eulerAngles = new Vector3 (tempRot.x, tempRot.y, tempRot.z);
transform.LookAt (target.transform);
if (tempPos.y < 3) {
tempPos.y = 3;
}
transform.position = tempPos;
transform.LookAt (target.transform);
}
float fov = Camera.main.fieldOfView;
fov -= Input.GetAxis("Mouse ScrollWheel") * sensitivity;
fov = Mathf.Clamp(fov, minFov, maxFov);
Camera.main.fieldOfView = fov;
}
}
You are an absolute legend. I can not tell you how many rabbit holes I've been down looking for this solution. Most are incomplete, ridiculously complex, only half the solution or just outright don't work. But this..it work straight away, its easy to read and understand. I can not thank you enough. Keep up the brilliant work.
Answer by Prasenjit_LinuxGuy · May 12, 2016 at 03:41 PM
Why the target is Null ?? In line 25
void LateUpdate ()
{
if (target != null) {
transform.LookAt (target.transform);
}
its not, I made 'target' public so that I can assign it in the inspector. I'm simply saying that if target is not null (ie has been assigned) then look at it. this script is a component of the camera.
Your answer
Follow this Question
Related Questions
Why does my transform.lookat not work? 1 Answer
How to make one movement direction be equal to diferent cameras? 2 Answers
Spyro Like Camera Follow 0 Answers
FPS camera rotating uncontrollably 0 Answers