如何在垂直轴上移动磁轭

时间:2018-11-05 12:50:22

标签: unity3d

我正在尝试沿垂直轴移动飞机叉架。我正在使用鼠标指针在垂直轴上上下移动磁轭并固定该值。当我运行脚本时,轭已定位到某个位置,并且不会上下移动。轭架没有上下移动。如图所示,如何使用以下代码上下移动磁轭。

yoke

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;

  namespace plane
  {
 public class IP_AirplaneThrottle_Physical : MonoBehaviour
  {
    #region Variables
    public float maxZOffset = -0.5f;
    public float sensitivity = 0.001f;
    public float smoothSpeed = 8f;

    public bool isHitting = false;
    public float wantedDelta;
    private Vector3 startPos;
    private Vector3 wantedPos;
    private Vector2 lastMousePosition;
    #endregion


    #region Builtin Methods
    // Use this for initialization
    void Start()
    {
        //Get the lever starting position
        startPos = transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        HandleRaycast();
        HandleInteraction();
    }
    #endregion



    #region Custom Methods
    void HandleRaycast()
    {
        //Build a ray so we can see if we are hitting the lever
        Ray curRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        //Do our Raycast into the scene
        if(Physics.Raycast(curRay, out hit, 1000f))
        {
            if(hit.transform.GetInstanceID() == this.transform.GetInstanceID())
            {
                Debug.Log("Hitting the Lever!");
                if(Input.GetMouseButtonDown(0))
                {
                    //We are hitting so get the start mouse position
                    isHitting = true;
                    lastMousePosition = Input.mousePosition;
                    print ("123");
                }
            }
        }

        //If we let go of the left mouse button then stop everything
        if(isHitting && Input.GetMouseButton(0) == false)
        {
            isHitting = false;
        }
    }

    void HandleInteraction()
    {
        if(isHitting)
        {
            //Calculate the delta for Z offset
            wantedDelta = (lastMousePosition.y - Input.mousePosition.y) * Time.deltaTime * sensitivity;
            startPos.z += wantedDelta;

            //make sure we dont go to far
            startPos.z = Mathf.Clamp(startPos.z, maxZOffset, 0f);
            wantedPos = startPos;

            //Get the New Mouse Position every frame while we are holding
            lastMousePosition = Input.mousePosition;



        }
        else
        {
            //Clear out the Delta value
            wantedDelta = 0f;
        }
                //Move the lever

             transform.position = Vector3.Lerp(transform.position, wantedPos, Time.deltaTime * smoothSpeed);


    }
    #endregion
}

}

0 个答案:

没有答案