C# 以慢动作移动游戏对象的位置#

C# 以慢动作移动游戏对象的位置#,c#,unity3d,C#,Unity3d,我想移动相机(游戏对象)就像在面板之间导航一样。。。它的工作很好,但我希望它是一个慢动作,像动画。我使用的是unity和c。 谢谢你的帮助 public class moveCanvas : MonoBehaviour { Vector3 reset = new Vector3(0, 0, -10); Vector3 leftpanel = new Vector3(-205.5f, 0, 0); Vector3 rightpanel = new Vector3(205.5

我想移动相机(游戏对象)就像在面板之间导航一样。。。它的工作很好,但我希望它是一个慢动作,像动画。我使用的是unity和c。 谢谢你的帮助

public class moveCanvas : MonoBehaviour
{
    Vector3 reset = new Vector3(0, 0, -10);
    Vector3 leftpanel = new Vector3(-205.5f, 0, 0);
    Vector3 rightpanel = new Vector3(205.5f, 0, 0);


    void Update()
    {

        if (Input.GetKeyDown(KeyCode.A))
        {
            GoLeft();
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            GoRight();
        }

    }

    public void GoLeft()
    {
        Vector3 currentposition = GameObject.Find("Main Camera").transform.position;
        if (currentposition == reset)
            GameObject.Find("Main Camera").transform.position = leftpanel;
        else if (currentposition == rightpanel)
            GameObject.Find("Main Camera").transform.position = reset;
        else
            return;

    }

    public void GoRight()
    {
        Vector3 currentposition = GameObject.Find("Main Camera").transform.position;
        if (currentposition == reset)
              GameObject.Find("Main Camera").transform.position = rightpanel;
        else if (currentposition == leftpanel)
            GameObject.Find("Main Camera").transform.position = reset;
        else
          return; 

    }
我找到了答案

using System.Collections;
using UnityEngine;

public class MoveCanvas : MonoBehaviour
{
    private Vector3 _reset = new Vector3(0, 0, -10);
    private Vector3 _leftpanel = new Vector3(-205.5f, 0, 0);
    private Vector3 _rightpanel = new Vector3(205.5f, 0, 0);
    private float _smoothTime = 0.7f;
    private Vector3 _velocity = Vector3.zero;
    private Vector3 _currentposition;
    private int _stoplimit = 2;

    void Start()
    {
        _currentposition = gameObject.transform.position;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            StartCoroutine(GoLeft());
        }

        else if (Input.GetKeyDown(KeyCode.D))
        {
            StartCoroutine(GoRight());
        }
    }

    private IEnumerator GoLeft()
    {
        if (_currentposition == _reset)
        {
            while (!Mathf.Approximately(_currentposition.x, _leftpanel.x))
            {
                gameObject.transform.position = Vector3.SmoothDamp(_currentposition, _leftpanel, ref _velocity, _smoothTime);
                _currentposition = gameObject.transform.position;
                if (Mathf.Abs((_currentposition.x) - (_leftpanel.x)) < _stoplimit)
                {
                    break;
                }
                yield return new WaitForEndOfFrame();
            }
            gameObject.transform.position = _leftpanel;
            _currentposition = _leftpanel;

        }

        else if (_currentposition == _rightpanel)
        {
            while (!Mathf.Approximately(_currentposition.x, _reset.x))
            {
                gameObject.transform.position = Vector3.SmoothDamp(_currentposition, _reset, ref _velocity, _smoothTime);
                _currentposition = gameObject.transform.position;
                if (Mathf.Abs((_currentposition.x) - (_reset.x)) < _stoplimit)
                {
                    break;
                }
                yield return new WaitForEndOfFrame();
            }
            gameObject.transform.position = _reset;
            _currentposition = _reset;

        }
    }

    private IEnumerator GoRight()
    {
        if (_currentposition == _reset)
        {
            while (!Mathf.Approximately(_currentposition.x, _rightpanel.x))
            {
                gameObject.transform.position = Vector3.SmoothDamp(_currentposition, _rightpanel, ref _velocity, _smoothTime);
                _currentposition = gameObject.transform.position;
                if (Mathf.Abs((_currentposition.x) - (_rightpanel.x)) < _stoplimit)
                {
                    break;
                }
                yield return new WaitForEndOfFrame();
            }
            gameObject.transform.position = _rightpanel;
            _currentposition = _rightpanel;

        }
        else if (_currentposition == _leftpanel)
        {
            while (!Mathf.Approximately(_currentposition.x, _reset.x))
            {
                gameObject.transform.position = Vector3.SmoothDamp(_currentposition, _reset, ref _velocity, _smoothTime);
                _currentposition = gameObject.transform.position;
                if (Mathf.Abs((_currentposition.x) - (_reset.x)) < _stoplimit)
                {
                    break;
                }
                yield return new WaitForEndOfFrame();
            }
            gameObject.transform.position = _reset;
            _currentposition = _reset;


        }
    }

}
使用系统集合;
使用UnityEngine;
公共类:单一行为
{
私有向量3 _reset=新向量3(0,0,-10);
私有向量3_leftpanel=新向量3(-205.5f,0,0);
私有向量3 _rightpanel=新向量3(205.5f,0,0);
私人浮动时间=0.7f;
专用矢量3 _速度=矢量3.0;
专用矢量3_当前位置;
私有整数_stoplimit=2;
void Start()
{
_currentposition=gameObject.transform.position;
}
无效更新()
{
if(Input.GetKeyDown(KeyCode.A))
{
开始例行程序(GoLeft());
}
else if(Input.GetKeyDown(KeyCode.D))
{
start例程(GoRight());
}
}
私人IEnumerator GoLeft()
{
如果(\u currentposition==\u reset)
{
而(!Mathf.近似(_currentposition.x,_leftpanel.x))
{
gameObject.transform.position=Vector3.SmoothDamp(\u currentposition,\u leftpanel,ref\u velocity,\u smoothTime);
_currentposition=gameObject.transform.position;
如果(Mathf.Abs((\u currentposition.x)-(u leftpanel.x))<\u stoplimit)
{
打破
}
返回新的WaitForEndOfFrame();
}
gameObject.transform.position=\u leftpanel;
_currentposition=_leftpanel;
}
else if(_currentposition==_rightpanel)
{
而(!Mathf.近似(_currentposition.x,_reset.x))
{
gameObject.transform.position=Vector3.SmoothDamp(当前位置、重置、参考速度、平滑时间);
_currentposition=gameObject.transform.position;
如果(数学绝对值((\u currentposition.x)-(\u reset.x))<\u停止极限)
{
打破
}
返回新的WaitForEndOfFrame();
}
gameObject.transform.position=\u重置;
_当前位置=_复位;
}
}
私人IEnumerator GoRight()
{
如果(\u currentposition==\u reset)
{
而(!Mathf.近似(_currentposition.x,_rightpanel.x))
{
gameObject.transform.position=Vector3.SmoothDamp(\u currentposition,\u rightpanel,ref\u velocity,\u smoothTime);
_currentposition=gameObject.transform.position;
如果(Mathf.Abs((\u currentposition.x)-(u rightpanel.x))<\u stoplimit)
{
打破
}
返回新的WaitForEndOfFrame();
}
gameObject.transform.position=_rightpanel;
_currentposition=_右面板;
}
else if(_currentposition==_leftpanel)
{
而(!Mathf.近似(_currentposition.x,_reset.x))
{
gameObject.transform.position=Vector3.SmoothDamp(当前位置、重置、参考速度、平滑时间);
_currentposition=gameObject.transform.position;
如果(数学绝对值((\u currentposition.x)-(\u reset.x))<\u停止极限)
{
打破
}
返回新的WaitForEndOfFrame();
}
gameObject.transform.position=\u重置;
_当前位置=_复位;
}
}
}

您检查过lerp或slerp吗?还有像这样的青少年。使用这些,您可以制作您喜欢的事务动画,使您的相机移动,而不是跳到新位置。您不需要执行
操作,否则在
void
函数中返回
。你唯一需要做的就是返回一个值,比如
else返回5。计算机将从函数返回,这就是为什么没有编译错误的原因。另外,你也可以把它全部放在一行上,不管怎样,我尝试了lerp和slerp,但都不起作用……谢谢你,但我没有得到答案