以慢动作统一c#移动游戏对象位置

时间:2018-05-07 14:54:26

标签: c# unity3d

我想移动相机(GameObject)就像在面板之间导航一样......它运作良好,但我想让它像动画一样慢动作。我正在使用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; 

    }

1 个答案:

答案 0 :(得分:0)

我找到了答案

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;


        }
    }

}