我正在開發一個移動 3D 應用程式。我有一個具有剛體的游戲物件,我用操縱桿移動它。
我得到了這樣的點,當我從操縱桿接收水平輸入時,我可以在垂直輸入時向前移動物件并旋轉它。
問題是,當我將剛體旋轉 90 度時,我希望使用水平輸入來向前或向后移動物件,并使用垂直操縱桿輸入來旋轉它。基本上我希望物體在操縱桿被拉動的地方向前移動,但是在當前設定下,即使物體面向左側或右側,我也必須向上拉操縱桿。我希望我解釋得很好。如果不知道,請告訴我,我會詳細解釋。
public Joystick m_Joystick;
private float m_HorizontalValue;
private float m_VerticalValue;
void Update()
{
m_VerticalValue = m_Joystick.Vertical;
m_HorizontalValue = m_Joystick.Horizontal;
}
// Same as update, but is used fo modifying values that are used to manipulate
// game object's physics
void FixedUpdate()
{
Move();
Turn();
}
void Move()
{
Vector3 movement = transform.forward * m_VerticalValue * m_Speed * Time.deltaTime;
m_Rigidbody.MovePosition(m_Rigidbody.position movement);
}
void Turn()
{
float turn = m_HorizontalValue * m_TurnSpeed * Time.deltaTime;
Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation);
}
我嘗試檢查剛體的前向向量(x 和 z 值)以在將操縱桿拉到所需方向時反轉物件的運動,但效果不佳,因為當 x 和 z 值向前時剛體會卡住向量相等。代碼不完整我只測驗了 x 和 z 坐標系的兩個四分之一(第一和第二個四分之一)。
float GetVerticalOrHorizaontalValueMovement()
{
float xValue = m_Rigidbody.transform.forward.x;
float zValue = m_Rigidbody.transform.forward.z;
float auxX = Mathf.Abs(xValue);
float auxZ = Mathf.Abs(zValue);
if (xValue > -0.1f && zValue > -0.1f)
{
if (auxX <= auxZ)
return m_HorizontalValue;
else if (auxX >= auxZ)
return m_VerticalValue;
}
else if(xValue > -0.1f && zValue < 0.1f)
{
if (auxX >= auxZ)
return m_VerticalValue;
else if (auxX <= auxZ)
return m_HorizontalValue;
}
return 0f;
}
float GetVerticalOrHorizaontalValueRotation()
{
float xValue = m_Rigidbody.transform.forward.x;
float zValue = m_Rigidbody.transform.forward.z;
float auxX = Mathf.Abs(xValue);
float auxZ = Mathf.Abs(zValue);
if (xValue > -0.1f && zValue > -0.1f)
{
if(auxX <= auxZ)
return m_VerticalValue;
else if(auxX >= auxZ)
return m_HorizontalValue;
}
else if(xValue > -0.1f && zValue < 0.1f)
{
if (auxX >= auxZ)
return m_HorizontalValue;
else if (auxX <= auxZ)
return m_VerticalValue;
}
return 0f;
}
這些方法將代替 Move() 和 Turn() 方法中的 m_VerticalValue 和 m_Horizo??ntalValue 變數放置。
我對任何可以讓我的游戲物件使用操縱桿隨心所欲地移動的解決方案持開放態度,但我必須將其保持為剛體,因為我想對它和其他可能的力施加爆炸力。如果我沒有正確解釋某些內容,請告訴我。
uj5u.com熱心網友回復:
如果有人需要它。這里是。使用移動操縱桿進行簡單的運動。如果您有復雜的動作,您可能需要根據需要對其進行修改
using UnityEngine;
public class MovementOfPlayer : MonoBehaviour
{
Vector3 movementInput;
Rigidbody playerRigidbody;
void Start()
{
playerRigidbody = GetComponent<Rigidbody>();
playerRigidbody.freezeRotation = true;
}
void FixedUpdate()
{
movementInput = Input.GetAxisRaw("Horizontal") * Vector3.right
Input.GetAxisRaw("Vertical") * Vector3.forward;
movementInput.Normalize();
float y = playerRigidbody.velocity.y;
if (movementInput != Vector3.zero)
{
if (transform.forward != movementInput)
{
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(movementInput), Time.deltaTime * 180);
playerRigidbody.velocity = Vector3.MoveTowards(playerRigidbody.velocity, Vector3.zero, Time.deltaTime * 30);
}
else
{
playerRigidbody.velocity = Vector3.MoveTowards(playerRigidbody.velocity, movementInput * 10, Time.deltaTime * 30);
}
}
else
{
playerRigidbody.velocity = Vector3.MoveTowards(playerRigidbody.velocity, Vector3.zero, Time.deltaTime * 30);
}
Vector3 velocity = playerRigidbody.velocity;
velocity.y = y;
playerRigidbody.velocity = velocity;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/329231.html
