快速開發實用工具VRTK-015

015_Controller_TouchpadAxisControl 

用Pad操作小車的前進和後退。按下扳機小車跳躍,按下的越多小車就會跳的越高。
核心腳本:RC_Car_Controller

首先,對Car添加控制腳本,計算Car的速度、方向和是否跳躍。

namespace VRTK.Examples
{
    using UnityEngine;

    public class RC_Car : MonoBehaviour
    {
        public float maxAcceleration = 3f;
        public float jumpPower = 10f;

        private float acceleration = 0.05f;
        private float movementSpeed = 0f;
        private float rotationSpeed = 180f;
        private bool isJumping = false;
        private Vector2 touchAxis;
        private float triggerAxis;
        private Rigidbody rb;
        private Vector3 defaultPosition;
        private Quaternion defaultRotation;

        public void SetTouchAxis(Vector2 data)
        {
            touchAxis = data;
        }

        public void SetTriggerAxis(float data)
        {
            triggerAxis = data;
        }

        public void ResetCar()
        {
            transform.position = defaultPosition;
            transform.rotation = defaultRotation;
        }

        private void Awake()
        {
            rb = GetComponent<Rigidbody>();
            defaultPosition = transform.position;
            defaultRotation = transform.rotation;
        }

        private void FixedUpdate()
        {
            if (isJumping)
            {
                touchAxis.x = 0f;
            }
            CalculateSpeed();
            Move();
            Turn();
            Jump();
        }

        private void CalculateSpeed()
        {
            if (touchAxis.y != 0f)
            {
                movementSpeed += (acceleration * touchAxis.y);
                movementSpeed = Mathf.Clamp(movementSpeed, -maxAcceleration, maxAcceleration);
            }
            else
            {
                Decelerate();
            }
        }

        private void Decelerate()
        {
            if (movementSpeed > 0)
            {
                movementSpeed -= Mathf.Lerp(acceleration, maxAcceleration, 0f);
            }
            else if (movementSpeed < 0)
            {
                movementSpeed += Mathf.Lerp(acceleration, -maxAcceleration, 0f);
            }
            else
            {
                movementSpeed = 0;
            }
        }

        private void Move()
        {
            Vector3 movement = transform.forward * movementSpeed * Time.deltaTime;
            rb.MovePosition(rb.position + movement);
        }

        private void Turn()
        {
            float turn = touchAxis.x * rotationSpeed * Time.deltaTime;
            Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
            rb.MoveRotation(rb.rotation * turnRotation);
        }

        private void Jump()
        {
            if (!isJumping && triggerAxis > 0)
            {
                float jumpHeight = (triggerAxis * jumpPower);
                rb.AddRelativeForce(Vector3.up * jumpHeight);
                triggerAxis = 0f;
            }
        }

        private void OnTriggerStay(Collider collider)
        {
            isJumping = false;
        }

        private void OnTriggerExit(Collider collider)
        {
            isJumping = true;
        }
    }
}

其次,對左右手柄添加CarController腳本,主要添加手柄事件映射,從而控制Car的運動。

 

namespace VRTK.Examples
{
    using UnityEngine;

    public class RC_Car_Controller : MonoBehaviour
    {
        public GameObject rcCar;
        private RC_Car rcCarScript;

        private void Start()
        {
            rcCarScript = rcCar.GetComponent<RC_Car>();
            GetComponent<VRTK_ControllerEvents>().TriggerAxisChanged += new ControllerInteractionEventHandler(DoTriggerAxisChanged);
            GetComponent<VRTK_ControllerEvents>().TouchpadAxisChanged += new ControllerInteractionEventHandler(DoTouchpadAxisChanged);

            GetComponent<VRTK_ControllerEvents>().TriggerReleased += new ControllerInteractionEventHandler(DoTriggerReleased);
            GetComponent<VRTK_ControllerEvents>().TouchpadTouchEnd += new ControllerInteractionEventHandler(DoTouchpadTouchEnd);

            GetComponent<VRTK_ControllerEvents>().ButtonTwoPressed += new ControllerInteractionEventHandler(DoCarReset);
        }

        private void DoTouchpadAxisChanged(object sender, ControllerInteractionEventArgs e)
        {
            rcCarScript.SetTouchAxis(e.touchpadAxis);
        }

        private void DoTriggerAxisChanged(object sender, ControllerInteractionEventArgs e)
        {
            rcCarScript.SetTriggerAxis(e.buttonPressure);
        }

        private void DoTouchpadTouchEnd(object sender, ControllerInteractionEventArgs e)
        {
            rcCarScript.SetTouchAxis(Vector2.zero);
        }

        private void DoTriggerReleased(object sender, ControllerInteractionEventArgs e)
        {
            rcCarScript.SetTriggerAxis(0f);
        }

        private void DoCarReset(object sender, ControllerInteractionEventArgs e)
        {
            rcCarScript.ResetCar();
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章