Reputation: 759
I am having a few strange issues with the way my camera is working. I am trying to do a perspective view of the player to allow for multiple layers to act like a parallaxing effect.
Here is the code I am using on the camera. ( I have dragged the Player onto the Transform target)
public Transform target;
public float distance = 3.0f;
public float height = 3.0f;
public float damping = 5.0f;
public bool followBehind = true;
public float rotationDamping = 10.0f;
void Update()
{
Vector3 wantedPosition;
if (followBehind)
wantedPosition = target.TransformPoint(0, height, -distance);
else
wantedPosition = target.TransformPoint(0, height, distance);
transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
}
If you look at the Scale it is set to .5 , but when I press play it looks like this:
My ultimate goal is to follow the player. Be some distance away and then be able to adjust the height of the camera, so that my player is towards the ground. Any help would be awesome.
Upvotes: 0
Views: 284
Reputation: 125245
wantedPosition
probably has a different z axis and is changing the z axis of your camera. I suggest you get the z axis of the camera then store it somewhere else. Always change the z axis of wantedPosition
to that default value before assigning it to the camera.
public Transform target;
public float distance = 3.0f;
public float height = 3.0f;
public float damping = 5.0f;
public bool followBehind = true;
public float rotationDamping = 10.0f;
float defaultZPos = 0;
void Start()
{
Vector3 tempCamPos = Camera.main.transform.position;
defaultZPos = tempCamPos.z;
}
void Update()
{
Vector3 wantedPosition;
if (followBehind)
wantedPosition = target.TransformPoint(0, height, -distance);
else
wantedPosition = target.TransformPoint(0, height, distance);
//Change the z pos to the deafult vale
wantedPosition.z = defaultZPos;
transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
}
Upvotes: 1