使用我目前的設定,我有一個通過空間向前移動的播放器,通過按鍵盤上的“A”和“D”,我可以在 x 軸上移動它。它看起來有點像地鐵沖浪者,除了你可以在 x 軸上自由移動,它不限于三行。所以這是我現在擁有的代碼......我已經嘗試了至少 5 種不同的方法,但似乎沒有任何效果。
public float speed = 5;
public Rigidbody rb;
public bool CanMove = false;
float horizontalInput;
[SerializeField] float horizontalMultiplier = 2;
Vector3 horizontalMove;
Vector3 RbPositionCheck;
private void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector3 touchPosition = Camera.main.ScreenToViewportPoint(touch.position);
Mathf.Round(touchPosition.x);
}
if (CanMove == true)
{
#region horizontalInput
horizontalInput = Input.GetAxis("Horizontal");
#endregion
#region Vector3 Calculations
Vector3 forwardMove = transform.forward * speed * Time.deltaTime;
horizontalMove = transform.right * horizontalInput * speed * Time.deltaTime * horizontalMultiplier;
#endregion
#region RbPositionCheck and Mathf.Clamp
RbPositionCheck = rb.position;
RbPositionCheck.x = Mathf.Clamp(RbPositionCheck.x, -4f, 4f);
#endregion
#region rb.MovePosition
rb.MovePosition(RbPositionCheck forwardMove horizontalMove);
#endregion
}
}
所以在代碼的上半部分有值和變數,我做了第一個 if 陳述句,但我不知道如何處理它......(它需要在觸摸屏上作業)我做了另一個完美地與計算機上的鍵盤配合使用。你能給我一些關于如何解決這個問題的提示或解決方案嗎?
uj5u.com熱心網友回復:
Input.GetAxis來自 InputManager,默認情況下它與觸摸輸入無關(我認為沒有選擇,也許某些插件可以使用它)。
您可以像在示例中那樣獲取 Touch,并存盤其位置,然后在下一幀(in Update)中,如果觸摸仍在進行中,您可以計算增量位置,并據此移動物件。
https://docs.unity3d.com/ScriptReference/Touch.html
https://docs.unity3d.com/ScriptReference/Touch-phase.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/523515.html
上一篇:我如何使這個運動腳本在C#中作業
