- Home /
Camera rotation around player while following.
I'm trying to achieve a camera that follows and rotates around the player while allowing the player to do things like rotate in C#. This means that the player and camera need to rotate separately, but I find it impossible to get the camera to rotate if I set it's position to follow the player. I know exactly what the problem with my code is, but I have no idea how to fix it! Please help!
public float turnSpeed = 0.40f;
public Transform player;
void Update()
{
transform.position = new Vector3(player.position.x, player.position.y + 8.0f, player.position.z + 7.0f);
transform.LookAt(player.position);
transform.RotateAround(player.transform.position, Vector3.up, Input.GetAxis("Mouse X") * turnSpeed);
}
The behavior sounds exactly like what you get with the standard $$anonymous$$ouseOrbit script provided by Unity.
Answer by robertbu · Dec 21, 2013 at 02:35 AM
Here are a couple of changes that give what I think you want:
using UnityEngine;
using System.Collections;
public class Orbit : MonoBehaviour {
public float turnSpeed = 4.0f;
public Transform player;
private Vector3 offset;
void Start () {
offset = new Vector3(player.position.x, player.position.y + 8.0f, player.position.z + 7.0f);
}
void LateUpdate()
{
offset = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
transform.position = player.position + offset;
transform.LookAt(player.position);
}
}
Hello @robertbu please can you tell me how to zoom the camera or make the orbit nearer to the player?
offset = new Vector3(player.position.x, player.position.y + 8.0f, player.position.z + 7.0f); change to offset = new Vector3(10,10,10); that is the zoom
$$anonymous$$ay you explain one thing to me, please? Why does quaternion in line 17 after being multiplied by vector3 becomes vector3? how does it work?
https://answers.unity.com/questions/372371/multiply-quaternion-by-vector3-how-is-done.html
https://answers.unity.com/questions/186252/multiply-quaternion-by-vector.html
https://stackoverflow.com/questions/58071118/how-to-multiply-a-quaternion-with-a-vector
https://en.m.wikipedia.org/wiki/Euler%E2%80%93Rodrigues_formula
Finally I found what I was looking for. I had the same problem and this works brilliantly, not even needed to adapt the code that much. That linecode (#17) makes the difference. I struggled for a long time to make a decent third person camera, and now I have that, thanks a lot for your contribution.
Answer by MrBalin · Sep 29, 2014 at 01:26 AM
HO HOHOHO Here's how to get the camera to rotate around the X axis or rather over and under the player.
using UnityEngine;
using System.Collections;
public class Orbit : MonoBehaviour {
public float turnSpeed = 4.0f;
public Transform player;
public float height = 1f;
public float distance = 2f;
private Vector3 offsetX;
private Vector3 offsetY;
void Start () {
offsetX = new Vector3 (0, height, distance);
offsetY = new Vector3 (0, 0, distance);
}
void LateUpdate()
{
offsetX = Quaternion.AngleAxis (Input.GetAxis("Mouse Y") * turnSpeed, Vector3.up) * offsetX;
offsetY = Quaternion.AngleAxis (Input.GetAxis("Mouse Y") * turnSpeed, Vector3.right) * offsetY;
transform.position = player.position + offsetX;
transform.LookAt(player.position);
}
}
So what did you do with offsetY exactly? I dont see it being used besides Quaternion.AngleAxis
He also used "$$anonymous$$ouse Y" for both the x and the Y axis. IF you are gonna help don't mess with people. It is best to just not help at all if you are. All this is going to do is frustrate new developers
Answer by alex13243 · Sep 01, 2015 at 11:08 AM
i used it too you need this modification of that
public float turnSpeed = 10f; public Transform player;
public float height = 1f;
public float distance = 2f;
private Vector3 offsetX;
private Vector3 offsetY;
void Start () {
offsetX = new Vector3 (0, height, distance);
offsetY = new Vector3 (0, 0, distance);
}
void LateUpdate()
{
offsetX = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offsetX;
offsetY = Quaternion.AngleAxis (Input.GetAxis("Mouse Y") * turnSpeed, Vector3.right) * offsetY;
transform.position = player.position + offsetX + offsetY;
transform.LookAt(player.position);
}
Answer by MrBalin · Sep 29, 2014 at 01:27 AM
I tried your suggestion but edited to fit my needs, rotates camera around public Transform variable "player", wherever that player object may be. This only rotates the camera around the x axis of the player's position. How can I raise the camera to slightly above the player's position?
Any suggestions on rotation on the Y axis?
using UnityEngine;
using System.Collections;
public class Orbit : MonoBehaviour {
public float turnSpeed = 4.0f;
public Transform player;
public float height = 1f;
public float distance = 2f;
private Vector3 offsetX;
void Start () {
offsetX = new Vector3(0, height, distance);
}
void LateUpdate()
{
offsetX = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offsetX;
transform.position = player.position + offsetX;
transform.LookAt(player.position);
}
}
Thank you so much this is exactly what I was looking for.