- Home /
Object lerping in same direction as camera jitters
I have an enemy the lerps around certain points while attacking, and that works all fine and dandy. But when the player's camera is moving the same direction as the enemy, and the enemy is right at the edge of the camera, the enemy starts to jitter like it's tying to stick inside the camera. This enemy is essentially just moving around to various points using lerp. I can't figure out what's causing this. I've tried putting the enemy update to late update like the camera, but that hasn't worked. Here's a video to show you guys what I mean. https://www.youtube.com/watch?v=49Af4DLd0dY&feature=youtu.be
Here's the camera script. If nobody can figure out while it's doing this i'll post the enemy script as well,but it's really messy and unorganized right now so i'll see if anybody can tell from this first.
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
public Controller2D target;
public Vector2 focusAreaSize;
public float verticalOffset;
public float lookAheadDistX;
public float lookSmoothTime;
public float VerticalSmoothTime;
private FocusArea focusArea;
private float currentLookAheadX;
private float targetLookAheadX;
private float lookAheadDirX;
private float smoothLookVelocityX;
private float smoothVelocityY;
private bool lookAheadStopped;
public void Start()
{
focusArea = new FocusArea(target.box.bounds, focusAreaSize);
}
public void LateUpdate()
{
focusArea.Update(target.box.bounds);
Vector2 focusPosition = focusArea.center + Vector2.up * verticalOffset;
if(focusArea.velocity.x != 0)
{
lookAheadDirX = Mathf.Sign(focusArea.velocity.x);
if(Mathf.Sign(target.playerInput.x) == Mathf.Sign(focusArea.velocity.x) && target.playerInput.x != 0)
{
lookAheadStopped = false;
targetLookAheadX = lookAheadDirX * lookAheadDistX;
}
else
{
if(!lookAheadStopped)
{
lookAheadStopped = true;
targetLookAheadX = currentLookAheadX + (lookAheadDirX * lookAheadDistX - currentLookAheadX) / 4f;
}
}
}
currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX,targetLookAheadX,ref smoothLookVelocityX,lookSmoothTime * Time.deltaTime);
focusPosition += Vector2.right * currentLookAheadX;
focusPosition.y = Mathf.SmoothDamp(transform.position.y,focusPosition.y,ref smoothVelocityY,VerticalSmoothTime * Time.deltaTime);
transform.position = (Vector3)focusPosition + Vector3.forward * -10;
}
public void OnDrawGizmos()
{
Gizmos.color = new Color(0,1,0,0.5f);
Gizmos.DrawCube(focusArea.center,focusAreaSize);
}
private struct FocusArea
{
public Vector2 center;
public Vector2 velocity;
private float left, right;
private float top, bottom;
public FocusArea(Bounds targetBounds, Vector2 size)
{
left = targetBounds.center.x - size.x / 2;
right = targetBounds.center.x + size.x / 2;
bottom = targetBounds.min.y;
top = targetBounds.min.y + size.y;
velocity = Vector2.zero;
center = new Vector2((left+right) / 2 , (top+bottom) / 2);
}
public void Update(Bounds targetBounds)
{
float shiftX = 0;
if(targetBounds.min.x < left)
{
shiftX = targetBounds.min.x - left;
}
else if(targetBounds.max.x > right)
{
shiftX = targetBounds.max.x - right;
}
left += shiftX;
right += shiftX;
float shiftY = 0;
if(targetBounds.min.y < bottom)
{
shiftY = targetBounds.min.y - bottom;
}
else if(targetBounds.max.y > top)
{
shiftY = targetBounds.max.y - top;
}
top += shiftY;
bottom += shiftY;
center = new Vector2((left+right) / 2 , (top+bottom) / 2);
velocity = new Vector2(shiftX,shiftY);
}
}
}
Your answer
Follow this Question
Related Questions
Camera Focus + Follow 0 Answers
2D Camera Smooth follow, FixedUpdate and LateUpdate odd difference, help needed. 1 Answer
Modified SmoothFollow 0 Answers
Locking onto Multiple AI's 1 Answer
why target switching of smooth camera not very smooth ? 0 Answers