How to move camera smoothly on trigger? 2D side scrolling,How to move camera on trigger enter? 2D side scroller project
I've been searching all morning and can't seem to find out how to do this. I am trying to have it so the camera is still, then when Player hits a trigger at the end of the screen, the camera will lerp over one screen. I just can't get the camera to move. I was finally getting close but the camera would just move jerky only a few pixels.
I was thinking I would use what you'd use for a smooth camera follow, but just have it occur on trigger and make it move from a to b.
Any help would be appreciated! Here's my current code... Total noob so don't judge to hard Also left my thoughts in there, not sure if i'm misguided on how to do this or not.
public Transform player;
public Transform mainCam;
public float smoothSpeed = 10f;
public Vector3 offset;
private void LateUpdate()
{
//If Player enters trigger and it's x value is less than the triggers value, then move the camera on the x axis + like 30 or something.
//If the Player's x value is higher than the Trigger's x value, then move the camera -30 on the x axis.
//Smooth lerp motion would be useful here.
}
private void screenMove()
{
if (player.position.x < -70f)
{
Debug.Log("Player hit screentrigger and it's x was lower than cams");
Vector3 desiredPosition = mainCam.position + offset;
Vector3 smoothedPosition = Vector3.Lerp(mainCam.transform.position, desiredPosition, smoothSpeed * Time.deltaTime);
mainCam.position = smoothedPosition;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
//if the collision tag is Player && the Player.x is less than the colliders x, then move camera right 30
//else move the camera left
if (collision.gameObject.tag == "Player")
{
screenMove();
}
}
}
At this point, it will move my camera to desired spot, but it is a quick jerk and I'm trying to get it to kinda pan over.
Your answer
Follow this Question
Related Questions
SmoothCameraCrouch 2 Answers
How to make an object go the direction it is facing? 0 Answers
How to Parent a Camera to the Head Bone with No Camera Shake? 0 Answers
Camera Tracking 1 Answer
Sit Down First person 0 Answers