如何在Unity 5.6中自动左右滑动对象?

时间:2017-07-04 18:35:05

标签: c# unity3d

我目前正在使用c#在Unity 3D中开发游戏。我开发了一些关卡,现在我想用自动移动立方体制作一些关卡(游戏的主题是立方体)。我在互联网上搜索了很多,但我找不到任何让我满意的东西。有人能帮我吗?我真的需要帮助。对不起,如果有一些语法错误。

1 个答案:

答案 0 :(得分:1)

创建一个名为SimpleTranslator.cs的新脚本,然后复制并粘贴以下代码。

using UnityEngine;

namespace TransformUtility
{
    public class SimpleTranslator : MonoBehaviour
    {
        [Tooltip("The local target position towards we translate this gameObject. A red line is drawn.")]
        public Vector3 m_localTargetPosition = new Vector3(0, 0, 5);
        public float speed = 1;
        public bool pingPong;
        public bool translateOnAwake = true;
        public new AudioSource audio;

        Vector3 m_initialPosition, m_targetPosition;
        Transform m_transform;

        void Awake()
        {
            m_transform = transform;
            m_initialPosition = m_transform.position;

            SetTargetPosition(m_localTargetPosition);
            enabled = translateOnAwake;
        }

        void FixedUpdate()
        {
            if (audio && !audio.isPlaying)
                audio.Play();

            m_transform.position = Vector3.MoveTowards(m_transform.position, m_targetPosition, speed * Time.deltaTime);

            if (m_transform.position == m_targetPosition)
            {
                if (pingPong)
                {
                    SwitchDirection();
                }
                else
                {
                    enabled = false;

                    if (audio)
                        audio.Stop();
                }
            }
        }

        public bool isTranslating
        {
            get
            {
                return enabled;
            }
        }

        public void SwitchDirection()
        {
            enabled = true;

            if (m_transform.position == m_initialPosition)
            {
                SetTargetPosition(m_localTargetPosition);
            }
            else
            {
                m_targetPosition = m_initialPosition;
            }
        }

        public void MoveToTargetPosition()
        {
            enabled = true;
            SetTargetPosition(m_localTargetPosition);
        }

        public void MoveToInitialPosition()
        {
            m_targetPosition = m_initialPosition;
            enabled = true;
        }

        public bool isInInitialPosition
        {
            get
            {
                return m_transform.position == m_initialPosition;
            }
        }

        public bool isInTargetPosition
        {
            get
            {
                return m_transform.position == m_initialPosition + m_transform.TransformDirection(m_localTargetPosition);
            }
        }

        private void SetTargetPosition(Vector3 localPosition)
        {
            m_targetPosition = m_initialPosition + transform.TransformDirection(localPosition);

#if UNITY_EDITOR
            m_endPositionDebug = m_targetPosition;
#endif
        }

#if UNITY_EDITOR
        Vector3 m_endPositionDebug;

        void OnDrawGizmos()
        {
            if (!Application.isPlaying)
            {
                Debug.DrawRay(transform.position, transform.TransformDirection(m_localTargetPosition), Color.red);
            }
            else
            {
                Debug.DrawLine(m_initialPosition, m_endPositionDebug, Color.red);
            }
        }
#endif
    }
}