- Home /
Camera following player but not locking on it
Hello! I've got my hands on a script and i can't seem to figure out why the camera isn't locking to the player. For example when i drop my player into the world the camera spawns behind the player but does not lock to the player. I have an empty gameobject inside the player that the "target" variable is assigned to in this script. I instantiate my player if anything has to do with that.
The problem is that if my player falls, the camera is still locked in the Y-position it has in the start.
IF! I hold the right mouse button though, there's no problem at all with the camera, which seems weird to me. I would want the right-click "function" to stick the camera in that position, but i'm too bad to do that. Anyway, here's the script
The script:
using UnityEngine; using System.Collections;
public class PlayerCameraScript : MonoBehaviour {
public Transform target;
public float distance;
public float cameraHeight;
private Transform _myTransform;
float x;
float y;
float xSpeed = 250.0f;
float ySpeed = 120.0f;
float heightDamping = 2.0f;
float rotationDamping = 3.0f;
// Use this for initialization
void Start () {
target = GameObject.FindWithTag ("CameraFocus").transform;
if(target == null) {
Debug.Log("No target assigned for the camera");
}
_myTransform = transform;
CameraSetup();
}
void LateUpdate() {
if(target != null) {
if(Input.GetMouseButton(1)) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
Quaternion rotation = Quaternion.Euler (y, x, 0);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
_myTransform.rotation = rotation;
_myTransform.position = position;
}
else {
x = 0;
y = 0;
//Calculate the current rotation angles
float wantedRotationAngle = target.eulerAngles.y;
float wantedHeight = cameraHeight;
float currentRotationAngle = _myTransform.eulerAngles.y;
//Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
//Damp the height
cameraHeight = Mathf.Lerp(cameraHeight, wantedHeight, heightDamping * Time.deltaTime);
//Convert the angle into a rotation
Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
//Set the position of the camera to a distance behind target
_myTransform.position = target.position;
_myTransform.position -= currentRotation * Vector3.forward * distance;
//Set the height of the camera
_myTransform.position = new Vector3(_myTransform.position.x, cameraHeight, _myTransform.position.z);
}
}
} public void CameraSetup() { _myTransform.position = new Vector3(target.position.x, target.position.y + cameraHeight, _myTransform.position.z - distance); _myTransform.LookAt(target); } }
It seems like this line:
_myTransform.position = new Vector3(_myTransform.position.x, cameraHeight, _myTransform.position.z);
is the problem, if i exclude it though, my camera is acting very weird...
See the comment I added to my answer. Have you tried adding _myTransform.position.y to the cameraHeight for the y component of your new vector?
Answer by Habitablaba · Sep 30, 2014 at 08:19 PM
This is the part that gets run if the right mouse button is held down:
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
Quaternion rotation = Quaternion.Euler (y, x, 0);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
_myTransform.rotation = rotation;
_myTransform.position = position;
If all you want is for that to happen all the time, regardless of if the mouse button is pressed or not, remove everything in the LateUpdate method and replace it just with the above lines.
If something else is your goal, you'll need to explain a little bit more about what you want to happen.
Yeah sorry I might not have been clear enough.
I want the camera to stay behind the player from the beginning, and it should follow the Player's back if i don't press the mousebutton, if i do, i want to be able to move the camera, it does that perfectly if i spawn the player at 0, 0, 0. But since i have Terrain i spawn the player at say 0 200, 0 or something,
There's the problem, the camera is stuck at 0 when the player is up at 200, so it doesn't actually follow the player in the Y-axis, for some odd reason, same thing if i spawn at 0 with the player and fall, the camera stays in that same position in the Y-axis.
If i press the right mouse button though, the camera jumps to the place it should be.
I hope I am clear enough with this. Thank you in advance
Try changing this:
//Set the height of the camera
_myTransform.position = new Vector3(_myTransform.position.x, cameraHeight, _myTransform.position.z);
to this:
//Set the height of the camera
_myTransform.position = new Vector3(_myTransform.position.x, _myTransform.position.y + cameraHeight, _myTransform.position.z);
Your answer
Follow this Question
Related Questions
Make object follow mouse, while actually moving camera, instead of object 2 Answers
Why can't I change my camera's position? 2 Answers
Many cameras with Camera.main.ScreenToWorldPoint trouble. 2 Answers
ScreenToWorldPoint for different ortographic size 1 Answer
Is there an elegant way of limiting camera's position? 1 Answer