I’m trying to find a way to have a camera follow the player but sit behind them whilst able to rotate with the player. Dragon Age style camera wouldn’t be too far from the mark…
So far, I’ve tried lookAt style scripts with no Avail… They automatically try and pin to the top even with a Vector3 offset, if I use transfom.position = target.position instead of LookAt it snaps to the player and won’t rotate with the player around the world. Attaching the camera with a click to move player end’s in a continuous spin. I’ve tried pretty much most of the scripts out of the Unit Script Wiki bank.
I’m nearly there, attach camera to main player… Found a script that I modified extensively to figure out exactly what I need out of the quaternion rotations:
Just need to clamp certain angles and maybe add a zoom… When making a game start from 0.0.0!! Causes a nightmare otherwise.
public Transform target;
float x = 0.0f;
float y = 0.0f;
public float walkDistance;
public bool camButtonDown;
public float runDistance;
public float height;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
// Use this for initialization
void Start () {
var player = GameObject.FindWithTag("Player");
target = player.transform;
//Vector3 angles = transform.eulerAngles;
//x = angles.y;
//y = angles.x;
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(1)) { //Use the Input Manager to make this user selectable button
camButtonDown = true;
}
if(Input.GetMouseButtonUp(1)) {
camButtonDown = false;
}
}
void LateUpdate(){
if(camButtonDown) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
// y = ClamAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
target.rotation = rotation;
}
else {
target.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance);
target.LookAt(target);
x = 0;
y = 0;
}
}
}
Are you trying to do a mouse orbit? Such that you can right click, and drag the camera up and down, and left and right around the character, while it’ll stay looking at the character? Maybe something like this would work:
Parent the camera to the player, so it’s position is relative to the player.
Put a script on the camera that has distance, hAngle (horizontal), and vAngle (vertical) float variables.
In that script, in Update(), set the transform position based on those variables, and update the forward vector so that it points that the player.
In that script, in OnGUI() look at the event and if it is a mouse event, get the mouse position, and subtract off the previously sampled position (if it wasn’t 0,0), and increase or decrease your angle variables accordingly. (Don’t forget that the angles are in radians, not that that really matters in this situation.)
If your character is walking on the X,Z plane, so that increasing Y is “up”, then try this:
in Update() …
// position the camera at a certain distance, horizontal angle and vertical angle from the player
transform.position = distance * (new Vector3(
Mathf.Cos(hAngle),
Mathf.Sin(vAngle),
Mathf.Sin(hAngle)
));
// make the camara look directly at the player
transform.forward = player.transform.position - transform.position;
Actually come to think of it, if the player is the camera’s parent, and the position of the camera is relative to the player, the player should always be at 0,0,0 in the camera’s reference frame, so you might have to set the forward vector to -1 * transform.position.normalized. That is,
That should make the camera point at 0,0,0 in its reference frame, which I would think would be where the player is, if the player is the parent of the camera.