【问题标题】:Move or lerp slowly game object to other game object's position by single click on UI Button not on multiple clicks? [duplicate]通过单击 UI 按钮而不是多次单击将游戏对象缓慢移动或缓慢移动到其他游戏对象的位置? [复制]
【发布时间】:2018-11-14 12:44:37
【问题描述】:

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

每当我点击那个按钮时,我的相机应该会慢慢地移动到那个空游戏对象的位置。

我的相机正在移动到那个位置,这很好,但问题是我必须多次点击那个按钮,直到我的相机到达它的位置。那么有没有办法通过单击该按钮将我的相机移动到游戏对象的位置?

这是我的代码:

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

【问题讨论】:

  • 你可以添加你的代码问题吗
  • 公共转换视图;公共浮动转换速度;变换当前视图;公共无效移动(){当前视图=视图; transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);
  • @derHugo 现在检查我的问题我的代码成功地对相机进行了处理,但是在多次单击 ui 按钮时,我希望在单击 ui 按钮时发生这种 lerping
  • 你怎么称呼这个移动函数可能问题出在那儿
  • @alikanat 我将此脚本附加到我的主摄像头,然后我将主摄像头拖到按钮的单击事件中,然后从那里调用此函数

标签: c# unity3d


【解决方案1】:

问题是您的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;
}

【讨论】:

  • 感谢这对我有用
  • 如果我只有 1 个按钮并将相机移动到只有单个对象的位置我有多个按钮和多个对象,当我按下按钮 1 它从一个位置移动到另一个位置时,您的这个协程解决方案工作正常按下按钮 2 它不动
  • 你必须修改它以使其适用于多个按钮
  • 请帮我修改一下,我会非常感谢你
  • 我设法将我的相机移动到所有对象位置,但是每当我的相机到达第三个游戏对象位置时,我的相机开始以慢速向另一个方向移动,我所做的就是将 isStarted = false 放入我的所有低于公共职能
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多