1

I want to move main camera slowly from one point to another point when a button is pressed. This button calls the method which has the code to move the camera.

I have tried using the Lerp method but the camera position is changing so fast that when I click on the button camera directly shifting to a new position. I want the camera to move slowly to a new position.

Below is my code, can anyone please help me with this.

=========================================================================

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

using UnityEngine;

public class Cameramove : MonoBehaviour
{
    public GameObject cam;
    Vector3 moveToPosition;// This is where the camera will move after the start
    float speed = 2f; // this is the speed at which the camera moves
    public void move()
    {
        //Assigning new position to moveTOPosition
        moveToPosition = new Vector3(200f, 400f, -220f);
        float step = speed * Time.deltaTime;
        cam.transform.position = Vector3.Lerp(cam.transform.position, moveToPosition, step);
        cam.transform.position = moveToPosition;
        
    }
    
}
sam
  • 127
  • 1
  • 5

2 Answers2

0

It's easier to use lerp with frames and you need to use it in an Update function. Try using the example from the Unity docs:

public int interpolationFramesCount = 45; // Number of frames to completely interpolate between the 2 positions
int elapsedFrames = 0;

void Update()
{
    float interpolationRatio = (float)elapsedFrames / interpolationFramesCount;

    Vector3 interpolatedPosition = Vector3.Lerp(Vector3.up, Vector3.forward, interpolationRatio);

    elapsedFrames = (elapsedFrames + 1) % (interpolationFramesCount + 1);  // reset elapsedFrames to zero after it reached (interpolationFramesCount + 1)

    Debug.DrawLine(Vector3.zero, Vector3.up, Color.green);
    Debug.DrawLine(Vector3.zero, Vector3.forward, Color.blue);
    Debug.DrawLine(Vector3.zero, interpolatedPosition, Color.yellow);
}
bpedazur
  • 363
  • 1
  • 9
-1

Try using smooth damp.

Here is the new code you should try:

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

using UnityEngine;

public class Cameramove : MonoBehaviour
{
    Vector3 matric;
    public GameObject cam;
    Vector3 moveToPosition;
    float speed = 2f; 
    bool move_ = false;

    void Update(){
         if(move_){
              //Assigning new position to moveTOPosition
              moveToPosition = new Vector3(200f, 400f, -220f);
              cam.transform.position = 
              Vector3.SmoothDamp(cam.transform.position,
                            moveToPosition,
                            ref matric, speed);
         }
    }

    public void move()
    {
        move_ = true;
    }
}
Holis
  • 175
  • 1
  • 10
  • Thanks for the answer, But when i click on the button camera is moving a bit, to reach the new position i have to click the button multiple times. is there a way to move the camera to new position smoothly just by clicking the button once? – sam Sep 25 '20 at 22:59
  • I edit the answer so it works. Sorry that the first time didn't work, I didn't realize that it needed to be on the Update method – Holis Sep 27 '20 at 23:58