Question by
idurvesh · Oct 01, 2015 at 04:49 PM ·
camera follow
Make camera follow player on curvey/turn path
Hi, i am using following script to follow player,The problem with this script is it do follow player properly when player's X position is fixed but as soon as player make turn to right or left on racing track the camera goes side of player instead of backside of player.
What can I add in my script that it will help Camera follow smoothly behind the player on turns too?
public class mySmoothFollow : MonoBehaviour
{
public Transform myTransform;
public Transform target;
public float distance = 20.0f;
public float height = 5.0f;
public float heightDamping = 2.0f;
public float lookAtHeight = 0.0f;
public float rotationSnapTime = 0.3F;
public float distanceSnapTime;
public Vector3 lookAtAdjustVector;
private float usedDistance;
float wantedRotationAngle, wantedRotationAngleX;
float wantedHeight;
float currentRotationAngle,currentRotationAnglex;
float currentHeight;
Quaternion currentRotation;
Vector3 wantedPosition;
private float yVelocity = 0.0F;
private float zVelocity = 0.0F;
float xVelocity = 0.0f;
void LateUpdate()
{
if ( target == null )
return;
if ( myTransform==null )
{
myTransform=transform;
}
wantedHeight = target.root.position.y + height;
currentHeight = myTransform.position.y;
wantedRotationAngle = target.parent.eulerAngles.y;
currentRotationAngle = myTransform.eulerAngles.y;
currentRotationAngle = Mathf.SmoothDampAngle(currentRotationAngle, wantedRotationAngle, ref yVelocity, rotationSnapTime);
wantedRotationAngleX = target.parent.eulerAngles.x;
currentRotationAnglex = myTransform.eulerAngles.x;
currentRotationAnglex = Mathf.SmoothDampAngle(currentRotationAnglex, wantedRotationAngleX, ref xVelocity, rotationSnapTime);
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
wantedPosition = target.root.TransformPoint(target.transform.localPosition);
wantedPosition.y +=10;
usedDistance = Mathf.SmoothDampAngle(usedDistance, distance, ref zVelocity, distanceSnapTime);
wantedPosition += Quaternion.Euler(currentRotationAnglex, currentRotationAngle, 0) * new Vector3(0, 0, -usedDistance);
myTransform.position = wantedPosition;
// myTransform.LookAt( target.position + lookAtAdjustVector ); } }
Comment
Your answer
