通过单击UI按钮而不是多次单击将游戏对象缓慢移动或束缚到其他游戏对象的位置?

时间:2018-11-14 12:44:38

标签: c# unity3d

我的场景中有一个按钮,一个主摄像头和一个空的游戏对象。

每当我单击该按钮时,我的相机就应缓慢地抓住该空白游戏对象的位置。

我的相机处于该位置,这很好,但是问题是我必须多次单击该按钮,直到相机到达其位置。那么,有没有一种方法可以通过单击该按钮将我的相机移动到游戏对象的位置?

这是我的代码:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

问题是您的Vector3.Lerp仅运行一次。尝试以下方法:

    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;
    }

当相机处于正确位置时,您必须想出一种方法来停止拖拉。因为按下按钮后,此if (flag == true)始终为真。

我相信这是使用协程的更好解决方案

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;
}