- Home /
How can I make the camera to orbit also up and down ?
The script is attached to the Main Camera.
Clamp I mean that it wont orbit down inside the floor and not orbit up back the player to limit it to 90 and -90 degrees on the Y I think.
This is the original script that work only to the left and right :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerFollow : MonoBehaviour
{
public Transform PlayerTransform;
private Vector3 _cameraOffset;
[Range(0.01f, 1.0f)]
public float SmoothFactor = 0.5f;
public bool LookAtPlayer = false;
public bool RotateAroundPlayer = true;
public float RotationsSpeed = 5.0f;
// Use this for initialization
void Start()
{
_cameraOffset = transform.position - PlayerTransform.position;
}
// LateUpdate is called after Update methods
void LateUpdate()
{
if (RotateAroundPlayer)
{
Quaternion camTurnAngle =
Quaternion.AngleAxis(Input.GetAxis("Mouse X") * RotationsSpeed, Vector3.up);
_cameraOffset = camTurnAngle * _cameraOffset;
}
Vector3 newPos = PlayerTransform.position + _cameraOffset;
transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);
if (LookAtPlayer || RotateAroundPlayer)
transform.LookAt(PlayerTransform);
}
}
This is what I tried and it seems to be working but also if I first move the camera to the left right if not and first moving up down it will move it opposite directions up will be down and down will be up only when moving it to the left and right then it fix the up down directions :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerFollow : MonoBehaviour
{
public Transform PlayerTransform;
private Vector3 _cameraOffset;
[Range(0.01f, 1.0f)]
public float SmoothFactor = 0.5f;
public bool LookAtPlayer = false;
public bool RotateAroundPlayer = true;
public float RotationsSpeed = 5.0f;
// Use this for initialization
void Start()
{
_cameraOffset = transform.position - PlayerTransform.position;
}
// LateUpdate is called after Update methods
void LateUpdate()
{
if (RotateAroundPlayer)
{
Quaternion camTurnAngle =
Quaternion.AngleAxis(Input.GetAxis("Mouse X") * RotationsSpeed, Vector3.up);
Quaternion camTurnAngle1 =
Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * RotationsSpeed, Vector3.right);
_cameraOffset = camTurnAngle * _cameraOffset;
_cameraOffset = camTurnAngle1 * _cameraOffset;
}
Vector3 newPos = PlayerTransform.position + _cameraOffset;
transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);
if (LookAtPlayer || RotateAroundPlayer)
transform.LookAt(PlayerTransform);
}
}
Answer by UnityedWeStand · Aug 05, 2020 at 12:00 AM
Quaternion camTurnAngle1 = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * RotationsSpeed, Vector3.right);
On line 35 you are rotating around the world x-axis. You need to be rotating around the camera's local x-axis, because that axis follows the camera and results in the right rotation no matter how much you have already rotated it left and right. Replace this line with:
Quaternion camTurnAngle1 = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * RotationsSpeed, transform.right);
Your answer
Follow this Question
Related Questions
Player not Moving in Test,Debugging not Working Correctly 0 Answers
Orbit Camera around object 2 Answers
Rotatearaound a moving object 1 Answer
"Follow Target" script with a prefab (C#) Help me please! 1 Answer
How can i track/update the lastPosition of a camera when switching between cameras ? 0 Answers