- Home /
Camera look position is always fixed.. need help.
I have a camera script that i've written for my RPG. The camera allows the player to click the right mouse button and look around. This feature works great. When you let go of the button the camera gently floats behind the player again. The problem is, when you click the right mouse button again, the camera always starts at a fixed rotation (0,0,0) no matter which way the character is turned. I need that rotation to always be looking at the player... Any help I can get would be great.
here's the script C#:
using UnityEngine;
using System.Collections;
public class RPGCamera : MonoBehaviour {
public Transform target;
public string playerTagName = "Player";
public float walkDistance;
public float runDistance;
public float height;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
public float heightDamping = 2.0f;
public float rotationDamping = 2.0f;
private Transform _myTransform;
private float _x;
private float _y;
private bool _camButtonDown = false;
void Awake(){
_myTransform = transform;
}
// Use this for initialization
void Start () {
if(target == null)
Debug.LogWarning("We do not have a target");
else{
CameraSetup();
}
}
void Update (){
if(Input.GetMouseButtonDown(1)){ //Use the Input Manager to make this user selectable.
_camButtonDown = true;
}
if(Input.GetMouseButtonUp(1)){
_camButtonDown = false;
}
if(Input.GetAxis("Mouse ScrollWheel") < 0){
walkDistance += 1;
}
if(Input.GetAxis("Mouse ScrollWheel") > 0){
walkDistance -= 1;
}
if(walkDistance > 10){
walkDistance = 10;
}
if(walkDistance < 5)
walkDistance = 5;
}
void LateUpdate(){
if(target != null){
if(_camButtonDown){
_x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
_y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
// _y = ClampAngle(_y, yMinLimit, yMaxLimit);
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 + height, target.position.z - walkDistance);
// _myTransform.LookAt(target);
_x = 0;
_y = 0;
// Calculate the current rotation angles
float wantedRotationAngle = target.eulerAngles.y;
float wantedHeight = target.position.y + height;
float currentRotationAngle = _myTransform.eulerAngles.y;
float currentHeight = _myTransform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
Quaternion currentRotation = Quaternion.Euler(0, 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, 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 + height, target.position.z - walkDistance);
_myTransform.LookAt(target);
}
}
Yeah, I'm sorry about that, it's one of a few that I'm still working on cleaning up. I need to get in the habit of organizing as I do it.
Answer by Waz · Aug 26, 2011 at 11:58 PM
You need to set _x and _y to current camera Euler angles when mouse is clicked.
worked great, just tossed in:
_x = transform.eulerAngles.y;
_y = transform.eulerAngles.x;
and it works great!
You're my new best friend! lol
Thank you!
It's slightly more efficient to get eulerAngles in one call:
var e = transform.eulerAngles;
_x = e.y;
_y = e.x;
Not a big deal though.
Your answer
Follow this Question
Related Questions
Code That Is Scene Dependent 1 Answer
camera script, allow free movement of the camera up to a certain distance. 1 Answer
Toggle Control between multiple turrets 0 Answers
A fast triangle triangle intersection algorithm for unity? 4 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers