Search Unity

Lerp Camera Position when Input

Discussion in 'Scripting' started by Giovane, Oct 17, 2019.

  1. Giovane

    Giovane

    Joined:
    Sep 23, 2019
    Posts:
    36
    It's there any way of lerping the camera position by inputting a button? I have a code that changes the camera pos but I want it to be smooth and I don't know how to make this.
    This is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ChangeCamera : MonoBehaviour
    6. {
    7.     public GameObject thirdPCamPos; //emptyObj
    8.     public GameObject firstPCamPos; //emptyObj
    9.     public GameObject cam;
    10.  
    11.     public string ChangeCam;        //InputKey
    12.     public bool is3P;               //is third person
    13.  
    14.     void Update()
    15.     {
    16.         if(Input.GetButtonDown (ChangeCam))
    17.         {
    18.             if(is3P == true)
    19.             {
    20.                 InvokeRepeating("CamToFirst", 0f, 0f);
    21.             }
    22.          
    23.             if(is3P == false)
    24.             {
    25.                 InvokeRepeating("CamToThird", 0f, 0f);
    26.             }
    27.  
    28.             is3P = !is3P;
    29.         }
    30.     }
    31. }
    32.  
    thanks in advance.
     
  2. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    567
    I am a noob here, but how about you translate the position and rotation of your Camera to where you want it to go (Like an empty game object), and just add Lerp/Slerp to it. Instead of switching between 2 cameras. Noob here though so maybe I dont know what I am talking about lol
     
    Giovane likes this.
  3. Giovane

    Giovane

    Joined:
    Sep 23, 2019
    Posts:
    36
    I'm not switching, I am changing the camera position to the emptys, as I said in the //, but I don't know how to lerp because I want it to change every time you tab the ChangeKey
     
  4. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    567
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour
    5. {
    6.     // Transforms to act as start and end markers for the journey.
    7.     public Transform startMarker;
    8.     public Transform endMarker;
    9.  
    10.     // Movement speed in units per second.
    11.     public float speed = 1.0F;
    12.  
    13.     // Time when the movement started.
    14.     private float startTime;
    15.  
    16.     // Total distance between the markers.
    17.     private float journeyLength;
    18.  
    19.     void Start()
    20.     {
    21.         // Keep a note of the time the movement started.
    22.         startTime = Time.time;
    23.  
    24.         // Calculate the journey length.
    25.         journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
    26.     }
    27.  
    28.     // Move to the target end position.
    29.     void Update()
    30.     {
    31.         // Distance moved equals elapsed time times speed..
    32.         float distCovered = (Time.time - startTime) * speed;
    33.  
    34.         // Fraction of journey completed equals current distance divided by total distance.
    35.         float fractionOfJourney = distCovered / journeyLength;
    36.  
    37.         // Set our position as a fraction of the distance between the markers.
    38.         transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fractionOfJourney);
    39.     }
    40. }
     
    Giovane likes this.
  5. Giovane

    Giovane

    Joined:
    Sep 23, 2019
    Posts:
    36
    Doesn't work, the camera does not go smoothly and it stops in the way to the position it should be because its a GetButtonDown input.
     
  6. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    567
    Maybe add time.Deltatime?
     
    Giovane likes this.
  7. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    567
    add "time.Deltatime" to these functions "CamToFirst". Sorry if I am not Much Help, Btw, might Also be Possible Using Animations
     
    Giovane likes this.
  8. Giovane

    Giovane

    Joined:
    Sep 23, 2019
    Posts:
    36
    still nothing and no one more experienced comes here to help
     
  9. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    567
    I only Started 2 weeks ago Despite how old my account is, and I have been stuck with every step of the way, but I must say, that every time I get stuck I learn so much.. I am actually stuck right now as we speak!
     
    Giovane likes this.
  10. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    567
    "
     
    Giovane likes this.
  11. Giovane

    Giovane

    Joined:
    Sep 23, 2019
    Posts:
    36
    This one is working really well, thank you man
     
    Last edited: Oct 17, 2019
    Mortalanimal likes this.
  12. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    567
    @Giovane, Your Welcome Bro, Btw I only come to the forum to ask for help if I cant find the Answer on youtube and in the Scripting reference first. I think if you do the same it may save you time
     
    Giovane likes this.