system
1
i have a 3rd person shooter game that i am trying to make and i am having problems with the camera. i want when i move th camera it moves the camera around my character, and the character to rotate so the camera is always looking at his back. how do i do that?
Ilya
2
I've created a small script for you, hope that helps:
var target : GameObject;
var followingDistance : float = 25.0;
private var rotationAngle : float = 0.0;
private var cameraY : float = 20.0;
var cameraSpeed = 3.0;
var targetRotationSpeed = 1.5;
function LateUpdate () {
//
//Make sure that the object is centered in camera and is always on the same distance
var mouseY = -5.0 * Input.GetAxis ("Mouse Y");
cameraY += mouseY;
//v = target - camera
var targetCameraPosition : Vector3 = -(target.transform.forward - transform.position).normalized * followingDistance + Vector3.up * cameraY;
//Rotate camera on plane xz
var mouseX = -5.0 * Input.GetAxis ("Mouse X");
rotationAngle += mouseX;
var rotationQuaternion1 : Quaternion = Quaternion.AngleAxis(rotationAngle, Vector3.up);
targetCameraPosition = rotationQuaternion1 * targetCameraPosition;
targetCameraPosition += target.transform.position;
targetCameraPosition = Vector3.Slerp(transform.position, targetCameraPosition, Time.deltaTime * cameraSpeed);
transform.position = targetCameraPosition;
//Make camera look at the object
var rotationQuaternion2 : Quaternion = Quaternion.LookRotation( (target.transform.position - transform.position).normalized );
transform.rotation = Quaternion.Slerp(transform.rotation, rotationQuaternion2, Time.deltaTime * cameraSpeed);
//
//Rotate target accordingly
//Make sure that the target will rotate only on xz plane
var xzRotationVector = (target.transform.position - transform.position).normalized;
xzRotationVector.y = 0.0;
var targetQuaternion : Quaternion = Quaternion.LookRotation( xzRotationVector );
target.transform.rotation = Quaternion.Slerp(target.transform.rotation, targetQuaternion, Time.deltaTime * targetRotationSpeed);
}