- Home /
Camera not facing back of character
Ok so my problem is as such. I have a character model in my game. The character model is the parent to an empty game object named CameraTarget which is being used as, you guessed it, the target for my camera.I have the script below attached to the main camera with the CameraTarget set as it's target.
The problem is that no matter what direction my character is facing, the CameraTarget is facing, the camera itself is facing when I click play rather than defaulting to the back of my characters head like I want it to, it always points off in the same direction. If I hold down the right mouse button down and look around and let go it snaps back to the same spot (as it should) but I find that this often times means it ends up snapping to the side of his face, or even the front of his face (depending on which way he is facing at the time.
How can I get the camera to realize it should be facing the BACK of the target?
using UnityEngine;
using System.Collections;
public class Camera : MonoBehaviour
{
public Transform target;
public string playerTagName = "Player";
public float walkDistance;
public float runDistance;
public float sprintDistance;
public float cameraHeight;
public float heightDampening = 2.0f;
public float rotationDampening = 3.0f;
private float _x;
private float _y;
public float xSpeed = 250f;
public float ySpeed = 120f;
private bool cameraButtonDown;
private Transform _myTransform;
void Awake()
{
_myTransform = transform;
}
// Use this for initialization
void Start ()
{
if(target == null)
Debug.Log("The camera does not have a target.");
else
{
CameraSetup();
}
}
void Update()
{
if(Input.GetMouseButtonDown(1)) //Use the input manager to make this user selectable
{
cameraButtonDown = true;
}
if(Input.GetMouseButtonUp(1)) //Use the input manager to make this user selectable
{
cameraButtonDown = false;
}
}
void LateUpdate()
{
if(target != null)
{
if(cameraButtonDown) //Use the input manager to make this user selectable
{
Debug.Log("Right Mouse");
_x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
_y -= Input.GetAxis("Mouse Y") * xSpeed * 0.02f;
Quaternion rotation = Quaternion.Euler (_y, _x, 0);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -walkDistance) + target.position;
_myTransform.rotation = rotation;
_myTransform.position = position;
}
else
{
_myTransform.position = new Vector3(target.position.x, target.position.y + cameraHeight, target.position.z - walkDistance);
_myTransform.LookAt(target);
_x = 0;
_y = 0;
float l_wantedRotationAngle = target.eulerAngles.y;
float l_wantedHeight = target.position.y + cameraHeight;
float l_currentRotationAngle = _myTransform.eulerAngles.y;
float l_currentHeight = _myTransform.position.y;
l_currentRotationAngle = Mathf.LerpAngle(l_currentRotationAngle, l_wantedRotationAngle, rotationDampening * Time.deltaTime);
l_currentHeight = Mathf.Lerp(l_currentHeight, l_wantedHeight, heightDampening * Time.deltaTime);
Quaternion currentRotation = Quaternion.Euler(0, l_currentRotationAngle, 0);
//Set the position of the camera on the x-z plane to:
//distance meters behind the target
_myTransform.position = target.position;
_myTransform.position -= currentRotation * Vector3.forward * walkDistance;
//Set the height of the camera
_myTransform.position = new Vector3(_myTransform.position.x, l_currentHeight, _myTransform.position.z);
//Always look at the target
_myTransform.LookAt(target);
}
}
else
{
GameObject go = GameObject.FindGameObjectWithTag(playerTagName);
if(go == null)
return;
target = go.transform;
}
}
public void CameraSetup()
{
_myTransform.position = new Vector3(target.position.x, target.position.y + cameraHeight, target.position.z - walkDistance);
_myTransform.LookAt(target);
}
}
I have tried using a different character model to see if it was something with the model, even though it didn't seem model related, and got the same result. Still can't figure out why this thing always wants to point the same direction. Still looking for help.
Your answer
Follow this Question
Related Questions
Rotate camera to character angle 0 Answers
Move character in direction its facing 1 Answer
How to limit the angle of the camera? 1 Answer
How To Set Camera Angle With A Variable? 0 Answers
Camera Follow Positive Y only - C# 1 Answer