using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraScript : MonoBehaviour
{
[SerializeField] private float sensitivityHor = 9.0f;
[SerializeField] private float sensitivityVert = 9.0f;
[SerializeField] private float minimumVert = -45.0f;
[SerializeField] private float maximumVert = 45.0f;
private float _rotationX = 0;
private Rigidbody PlayerRigidbody;
void Start()
{
PlayerRigidbody = GetComponent<Rigidbody>();
if (PlayerRigidbody != null)
{
PlayerRigidbody.freezeRotation = true;
}
}
void Update()
{
_rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
_rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
float delta = Input.GetAxis("Mouse X") * sensitivityHor;
float rotationY = transform.localEulerAngles.y delta;
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
}
}
晚上好。我寫了一個腳本,通過在螢屏上滑動手指(它在我的相機上)來旋轉相機,用一根手指一切正常,但是如果你用兩根手指同時觸摸,應用程式會做出錯誤的反應(突然更改相機旋轉)。如何使用 Input.GetAxis 修復它,或者我可以使用什么來撰寫用于多次觸摸的腳本?
uj5u.com熱心網友回復:
您可以使用只有第一個觸摸你的運動,所以,如果有第二個什么也不會發生變化,并Input.GetAxis("Mouse Y")通過Input.GetTouch(0).deltaPosition.y與X和Y分別。像這樣:
if (Input.touchCount > 0) {
_rotationX -= Input.GetTouch(0).deltaPosition.y * sensitivityVert;
_rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
float delta = Input.GetTouch(0).deltaPosition.x * sensitivityHor;
float rotationY = transform.localEulerAngles.y delta;
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
}
代碼未除錯,因為它只是您的替換代碼。如果處理第一次觸摸的行為很奇怪,您也許可以處理中間點的旋轉。像這樣:
//get the touch middle when the second finger touches
if (Input.GetTouch(1).phase == TouchPhase.Began) {
touchMiddle = (Input.GetTouch(0).position
Input.GetTouch(1).position) / 2;
touchDistSqr = (Input.GetTouch(0).position -
Input.GetTouch(1).position).sqrMagnitude;
return;
}
if (Input.touchCount == 2) {
var deltaMidde = touchMiddle - (Input.GetTouch(0).position Input.GetTouch(1).position) / 2;
transform.localEulerAngles = new Vector3(deltaMidde.x, deltaMidde.y, 0);
}
中的所有代碼LateUpdate()。最好相機移動需要LateUpdate()在 Update 中完成,以使可能在 Update 中移動的物件在每一幀都處于其最終狀態。根據檔案中的建議。不是很相關,但請注意,您可以在大多數情況下使用Vector2而不是Vector3螢屏操作。
此外,在UpdateorLateUpdate操作中將它們相乘以Time.deltaTime使移動與幀速率無關是非常有趣的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/408535.html
標籤:
上一篇:JSforEach-按加載順序而不是隨機處理影像檔案
下一篇:檢查一個值是否介于兩個旋轉值之間
