-1

I want to smoothly translate the position of the camera to the position of the second player but Vector3.Lerp isn't working. I also want the size of the camera to smoothly go from 10 to 5 but Mathf.Lerp isn't working. Finally, I want the scale of the second player to go from 0.1 to 0.2.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Win: MonoBehaviour
    {
        public GameObject secondPlayer;
        public GameObject firstPlayer;
        public Camera cam;


        private void OnCollisionEnter2D(Collision2D collision)
        {
            Vector2 scale = new Vector2(0.2f, 0.2f);
            Vector3 positionOfSecondPlayer = new Vector3(secondPlayer.transform.position.x, secondPlayer.transform.position.y, -10);
            if (collision.gameObject.layer == 3)
            {
                cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, 5, 45);
                cam.transform.position = Vector3.Lerp(secondPlayer.transform.position, positionOfSecondPlayer, 45);
                Destroy(firstPlayer);
                secondPlayer.transform.localScale = Vector2.Lerp(secondPlayer.transform.localScale, scale, 45);
            }
        }
    }
the_boy
  • 3
  • 2
  • 1
    You should only use values between 0 and 1 for the `t` argument of `Lerp`, otherwise it will treat it as 0 or 1. What is the meaning of 45? – Ruzihm Mar 20 '21 at 04:40
  • 1
    Also, Lerp only calculates an interpolation once when it is called. If you want to have a lerp occur over time, you would need to call it multiple times, such as in a coroutine. – Ruzihm Mar 20 '21 at 04:41
  • Maybe [this](https://stackoverflow.com/a/37228628/1092820) will help – Ruzihm Mar 20 '21 at 04:49
  • It didn't really help but thanks anyway. – the_boy Mar 20 '21 at 04:57

1 Answers1

0

As mentioned in the comments this is not how you use Marhf.Lerp or Vector3.Lerp. It is a one time interpolation using a start and end value and a factor between 0 and 1.

As also mentioned you would rather use a Coroutine like e.g.

    // Configure the desired animation duration via the Inspector in seconds
    [SerializeField] private float lerpDuration = 1;

    // Making this return IEnumerator automatically runs it as a Coroutine!
    private IEnumerator OnCollisionEnter2D(Collision2D collision)
    {
        // Instead of a layer check you can configure the CollisionMatrix and anyway only
        // allow collisions between certain layers btw
        if (collision.gameObject.layer != 3) yield break;

        Destroy(firstPlayer);
        
        // Cache all your start and target values beforehand
        var targetScale = Vector2.one * 0.2f;
        var startPosition = secondPlayer.transform.position;
        var targetPosition = startPosition;
        targetPosition.z = -10;
        var startScale = secondPlayer.transform.localScale;
        
        var camStartSize = cam.orthographicSize;

        var timePassed = 0f;
        while(timePassed < lerpDuration)
        {
            // This factor will move from 0 to 1 within lerpDuration seconds
            var factor = timePassed / lerpDuration;
            // Optional add ease-in and ease-out
            factor = Mathf.SmoothStep(0,1,factor);

            // Now do the interpolations Frommstart to taget values using the factor
            cam.orthographicSize = Mathf.Lerp(camStartSize, 5, factor);
            cam.transform.position = Vector3.Lerp(startPosition, targetPosition, factor);
            secondPlayer.transform.localScale = Vector2.Lerp(startScale, targetScale, factor);

            // Increase by the time passed since last frame 
            timePassed += Time.deltaTime;
            // Tell Unity to "pause" the routine here, render this frame
            // and continue from here in the next frame
            yield return null;
        }

        // Just to have clean values set them hard when done
        cam.orthographicSize = 5;
        cam.transform.position = targetPosition;
        secondPlayer.transform.localScale = scale;
    }
derHugo
  • 83,094
  • 9
  • 75
  • 115