Hi Friends,
I’m trying to script the camera so that as the main character moves (in a 2.5d platformer) the camera shifts forward so more of the oncoming level is visible to the player. If the player runs, the camera shifts forward even more.
I have it implemented and working, however there’s a choppy sort of visual effect going on.
I’m using SmoothDamp (Lerp didn’t help much).
Here’s the code for the camera:
using UnityEngine;
using System.Collections;
public class CameraMovement : MonoBehaviour {
public PlayerMovement playerMove;
private Transform player;
private Vector3 camPos;
private Vector3 tempPos;
private Vector3 velocity = Vector3.zero;
// Use this for initialization
void Awake() {
camPos = this.transform.position;
playerMove = GetComponent ();
player = GameObject.FindGameObjectWithTag (“Player”).transform;
}
// Update is called once per frame
void FixedUpdate () {
Vector3 playerpos = player.position;
Vector3 curPos = transform.position;
if (playerMove.movingLeft) {
if (playerMove.running) {
tempPos = camPos + (playerpos - new Vector3 (8.0f, 0.0f, 0.0f));
}else{
tempPos = camPos + (playerpos - new Vector3 (4.0f, 0.0f, 0.0f));
}
} else if (playerMove.movingRight) {
if (playerMove.running) {
tempPos = camPos + (playerpos - new Vector3 (-8.0f, 0.0f, 0.0f));
}else{
tempPos = camPos + (playerpos - new Vector3 (-4.0f, 0.0f, 0.0f));
}
} else {
tempPos = camPos + playerpos;
}
transform.position = Vector3.SmoothDamp (curPos, tempPos, ref velocity, 0.25f);
}
}
Does anyone know how I can get rid of the choppy side effect?