- Home /
Why is my camera switching angles?
Ok so I have been following along with the Hack and Slash tutorial series at BurgZergArcade.com
I have gone through the step of creating a camera script to follow our player and allow for right click features.
Either I(we) messed something up in the code or it simply isn't what I am wanting from the script.
when I right click the camera always switches angles to face the same direction no matter what direction my character is facing. I want it to always lock on a behind view placing the back of my character on the screen when I right click as well as be able to turn the character by moving the mouse on the x axis. The latter might have to be done in a diff script, not sure. anyways here is the code. Can anyone explain to me why the camera is locking on to that specific direction when I right click instead of locking on to the back of my character?
using UnityEngine; using System.Collections;
public class HackAndSlashCamera : MonoBehaviour { public Transform target; public string playerTagName = "Player";
public float minDistance;
public float maxDistance;
public float height;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
public float heightDamping = 2.0f;
public float rotationDamping = 3.0f;
private Transform _myTransform;
private float _x;
private float _y;
private bool _cameraButtonDown = false;
void Awake()
{
_myTransform = transform;
}
// Use this for initialization
void Start ()
{
if(target == null)
Debug.LogWarning("We do not have a target");
else
{
CameraSetup();
}
}
// Character moves in the update
void Update ()
{
if(Input.GetMouseButtonDown(1))
{
_cameraButtonDown = true;
}
if(Input.GetMouseButtonUp(1))
{
_cameraButtonDown = (false);
}
}
// Camera moves in the late update
void LateUpdate()
{
if(target != null)
{
if(_cameraButtonDown) //Use the Input Manager to make this a user selectable button.
{
//Debug.Log("Right Mouse");
_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, -minDistance) + target.position;
_myTransform.rotation = rotation;
_myTransform.position = position;
}
else
{
//controls the camera location
//_myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - minDistance);
//Makes camera look at the target.
//_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 * minDistance;
// 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()
{
//controls the camera location
_myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - minDistance);
//Makes camera look at the target.
_myTransform.LookAt(target);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Toggle Control between multiple turrets 0 Answers
This is a sloppy conversion from javascript to C#, I cannot seem to get it working... 2 Answers
problem wtih the script attachment 0 Answers
How do I rotate a Rigidbody to a surface normal AND use camera rotation? 1 Answer