- Home /
Rotate camera around player
I'm trying to get my camera to rotate around my player on a button press however I've only been able to get it to rotate on its own axis and not around the players position. Any ideas how I'd go about it in my script.
using UnityEngine;
using System.Collections;
public class MouseOrbit : MonoBehaviour
{
public Transform target;
public float distance = 10.0f;
private float x = 0.0f;
private float y = 0.0f;
void Start ()
{
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
void Update ()
{
if (target)
{
transform.position = (Quaternion.Euler(y, x, 0)) * new Vector3(0.0f, 0.0f, -distance) + target.position;
}
if (Input.GetButton("CameraRotate"))
{
transform.rotation = Quaternion.Euler(y, x, 0);
}
if (Input.GetAxis("MouseScrollWheel") > 0)
{
if (distance >= 2)
{
distance = distance - 0.25f;
}
}
if (Input.GetAxis("MouseScrollWheel") < 0)
{
if (distance <= 10)
{
distance = distance + 0.25f;
}
}
}
}
The target = player.
Answer by robertbu · Jan 12, 2014 at 05:44 PM
I'm confused some about what you are trying to do with parts of your code. In particular the 'Input.GetButton("CameraRotate"). Anyway, here is a basic MouseOrbit script that you can build on. Note the default axis name for the scroll wheel is 'Mouse ScrollWheel'. Not sure if you changed the name or if there is a bug here.
sing UnityEngine; using System.Collections;
public class MyOrbitScript : MonoBehaviour {
public Transform target;
public float distance = 10.0f;
public float sensitivity = 3.0f;
private Vector3 offset;
void Start () {
offset = (transform.position - target.position).normalized * distance;
transform.position = target.position + offset;
}
void Update () {
Quaternion q = Quaternion.AngleAxis(Input.GetAxis ("Mouse ScrollWheel") * sensitivity, Vector3.up);
offset = q * offset;
transform.rotation = q * transform.rotation;
transform.position = target.position + offset;
}
}
The mouse scroll is/was fine it just increases/decreases the distance between the camera and player. The 'Input.GetButton("CameraRotate")' is where I am trying to set the rotation of the camera around the player whenever CameraRotate is pressed which is set in my Input settings to Q.
Thanks for the answer it really works well. Is there a way to make it smoother?
I'm using this as the groundwork for my rotation script. Thanks a thousand!
Answer by Lagger625 · May 18, 2018 at 12:06 AM
I want to share my script to make the camera follow and rotate around the player. It even supports zooming in and out with the mouse wheel, all the settings exposed in the editor, and a mechanism to avoid seeing through the objects!
using UnityEngine;
public class PlayerCamera : MonoBehaviour {
[SerializeField]
private float turnSpeed = 4.0f, distanceFromPlayer = 3f, minDistance = 1f, maxDistance = 10f, zoomStep = .5f;
[SerializeField]
private Transform player;
[SerializeField]
private Vector3 startingRotation = new Vector3(20f, 0f, 0f);
private Vector3 rotation;
// couldn't find a better name...
// distance to offset camera position to help avoid seeing through the floor or walls
// still not a perfect mechanism
private float onRaycastHitOffset = .7f;
private void Start() {
rotation = startingRotation;
}
private void LateUpdate () {
Vector3 newPosition = player.position;
distanceFromPlayer = Mathf.Clamp(distanceFromPlayer - Input.mouseScrollDelta.y * zoomStep, minDistance, maxDistance);
transform.rotation = Quaternion.Euler(rotation += new Vector3(-Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X")) * turnSpeed);
// check if there is a collider between camera and player to move camera closer to the player
RaycastHit hit;
Ray ray = new Ray(newPosition, -transform.forward);
if (Physics.Raycast(ray, out hit, distanceFromPlayer * (1f / onRaycastHitOffset))) {
newPosition -= transform.forward * hit.distance * onRaycastHitOffset;
} else {
newPosition -= transform.forward * distanceFromPlayer;
}
transform.position = newPosition;
}
}