0

I have a button in my scene, a main camera and an empty game object.

Whenever I click on that button my camera should slowly lerp to that empty game object's position.

My camera is lerping to that position, which is fine, but the problem is that I have to click multiple times on that button until my camera reach its position. So is there a way to move my camera to game object's position using a single click on that button?

Here is my code:

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

public class TransitionCAM : MonoBehaviour {
    public Transform views;
    public float transitionSPEED;
    Transform currentVIEW;

    public void move(){
        currentVIEW = views;
        transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);

        //for camera rotation
        Vector3 currentangel = new Vector3 ( Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, Time.deltaTime *transitionSPEED),
            Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, Time.deltaTime *transitionSPEED),
            Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, Time.deltaTime *transitionSPEED));

        transform.eulerAngles = currentangel;
    }
}
pushkin
  • 9,575
  • 15
  • 51
  • 95
Nouman Khan
  • 49
  • 2
  • 11
  • can you add your code the question, please – derHugo Nov 14 '18 at 12:57
  • public Transform views; public float transitionSPEED; Transform currentVIEW; public void move(){ currentVIEW = views; transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED); – Nouman Khan Nov 14 '18 at 13:06
  • @derHugo check my question now my code is successfuly lerping the camera but on multiple clicks on ui button i want this lerping to be happen on a single click on ui button – Nouman Khan Nov 14 '18 at 13:14
  • How do you call this move function probably the problem is there – Ali Kanat Nov 14 '18 at 13:19
  • @alikanat i attach this script to my main camera then i drag my main camera into the on click event of button and from there i call this function – Nouman Khan Nov 14 '18 at 13:24
  • How about starting `Coroutine` on button pressed? – Morasiu Nov 14 '18 at 13:48
  • @Morasiu can u modify the above code for me please if u can thanks – Nouman Khan Nov 14 '18 at 13:52
  • @alikanat ok wait i am going to try this out – Nouman Khan Nov 14 '18 at 14:03
  • @alikanat btw if i cant call this function using UI button my lerp work fine its just a button which i have to press everytime – Nouman Khan Nov 14 '18 at 14:05
  • hey @alikanat love u so much man u saved me thanks man – Nouman Khan Nov 14 '18 at 14:11
  • @NoumanKhan :) you are welcome glad i could help. You can also check `Coroutine` it might be a better solution to this. – Ali Kanat Nov 14 '18 at 14:13
  • @alikanat actually im new to this C# programming that's why i have less knowledge if you are free you can write the coroutine solution if u are free thanks – Nouman Khan Nov 14 '18 at 14:16
  • @NoumanKhan I edit the solution with a coroutine solution if you want to slow it down you can change `yield return null` part – Ali Kanat Nov 14 '18 at 14:32
  • @AliKanat ur scripts are working fine now i have multiple objects in view and multiple ui buttons now what i want that everytime i click a different button the camera goes to different objects location how can i achieve by editing this code – Nouman Khan Nov 14 '18 at 17:42
  • Well you should have two public objects and two functions like `moveObjLeftButton` and `moveObjRightButton` then each function you should have a variable to decide which public object's position you will use. That is it rest is same – Ali Kanat Nov 14 '18 at 18:14
  • @AliKanat ok dear i will try and will tell you if i unable to do this and thanks again for ur help and precious time thanks man God bless u – Nouman Khan Nov 14 '18 at 18:25

1 Answers1

0

Problem is your Vector3.Lerp runs only once. Try this instead:

    public Transform views;
    public float transitionSPEED;
    Transform currentVIEW;
    private bool flag = false;

    Vector3 currentangel;
    private void Start()
    {
        Debug.Log(flag);
        currentVIEW = views;

    }
    private void Update()
    {
        if (flag == true)
        {

            transform.position = Vector3.Lerp(transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);
            Debug.Log("object pos" + transform.position);
            Debug.Log(currentVIEW.position);
            //for camera rotation

            currentangel = new Vector3(Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, Time.deltaTime * transitionSPEED),
            Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, Time.deltaTime * transitionSPEED),
            Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, Time.deltaTime * transitionSPEED));

            transform.eulerAngles = currentangel;
        }
    }



    public void Move()
    {
        flag = true;
    }

You have to come up with a way to stop lerping when your camera is at correct position. Because this if (flag == true) will be always true after button is pressed.

This is a better solution i believe using Coroutine

public Transform views;
public float transitionSPEED;
Transform currentVIEW;
private bool flag = false;
private bool isStarted = false;
Vector3 currentangel;
private void Start()
{
    Debug.Log(flag);
    currentVIEW = views;

}
private void Update()
{
    if (flag  && !isStarted)
    {
        StartCoroutine(CoroutineSolution());
        isStarted = true;
    }
}
IEnumerator CoroutineSolution()
{
    float t = 0.0f;
    while ( t<1.0f )
    {

        t += Time.deltaTime;
        transform.position = Vector3.Lerp(transform.position, currentVIEW.position, t);

        //for camera rotation

        currentangel = new Vector3(Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, t),
        Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, t),
        Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, t));

        transform.eulerAngles = currentangel;
        Debug.Log("Coroutine running");

        yield return null;

    }

}


public void Move()
{
    flag = true;
}
Ali Kanat
  • 1,888
  • 3
  • 13
  • 23
  • thanks man this worked for me – Nouman Khan Nov 14 '18 at 14:12
  • your this Coroutine Solutions works fine if i have only 1 button and move camera to only single objects position i have multiple buttons and multiple objects when i press button 1 it moves from one position to another but when i press button 2 it doesnot move – Nouman Khan Nov 27 '18 at 14:08
  • Well you have to modify it to make it work for multiple buttons – Ali Kanat Nov 27 '18 at 15:53
  • please modify it for me please i will be very thankful to u – Nouman Khan Nov 27 '18 at 16:06
  • i manage to move my camera to all objects position but whenever my camera reaches at third game object position my camera starts to move in another direction with slow speed what i did is put isStarted = false in my all below public functions – Nouman Khan Nov 27 '18 at 17:21