当我按下其中一个轴时,我想集成刚体运动学,因为在我的代码中,当我的坦克移动后,它会继续移动一段时间,就像它是一个力一样。我试图添加它,如果它停止按下按钮,但它没有工作。
我试图在更新中添加它们,或者以布尔值的形式添加,但是我认为我在设置逻辑位置以便正确工作方面存在问题。
我想要的是当释放轴时激活对象的运动学。
非常感谢你的帮助
public class TankController : MonoBehaviour
{
public int m_PlayerNumber = 1;
public float m_Speed = 12f;
public float m_TurnSpeed = 180f;
public AudioSource m_MovementAudio;
public float m_PitchRange = 0.2f;
private string m_MovementAxisName;
private string m_TurnAxisName;
private Rigidbody m_Rigidbody;
private float m_MovementInputValue;
private float m_TurnInputValue;
private float m_OriginalPitch;
private void Awake()
{
m_Rigidbody = GetComponent<Rigidbody>();
}
private void OnEnable()
{
// When the tank is turned on, make sure it's not kinematic.
m_Rigidbody.isKinematic = false;
// Also reset the input values.
m_MovementInputValue = 0f;
m_TurnInputValue = 0f;
}
private void OnDisable()
{
// When the tank is turned off, set it to kinematic so it stops moving.
m_Rigidbody.isKinematic = true;
}
private void Start()
{
// The axes names are based on player number.
m_MovementAxisName = "CarroV" ;
m_TurnAxisName = "CarroH";
// Store the original pitch of the audio source.
}
private void Update()
{
// Store the value of both input axes.
m_MovementInputValue = Input.GetAxis(m_MovementAxisName);
m_TurnInputValue = Input.GetAxis(m_TurnAxisName);
EngineAudio();
}
private void FixedUpdate()
{
// Adjust the rigidbodies position and orientation in FixedUpdate.
/* if (Input.GetKey(KeyCode.Keypad8) || !Input.GetKey(KeyCode.Keypad2) || !Input.GetKey(KeyCode.Keypad4) || !Input.GetKey(KeyCode.Keypad6))
{
m_Rigidbody.isKinematic = true;
}
*/
Move();
Turn();
}
private void Move()
{
//m_Rigidbody.isKinematic = false;
// Create a vector in the direction the tank is facing with a magnitude based on the input, speed and the time between frames.
Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime;
// Apply this movement to the rigidbody's position.
m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
}
private void Turn()
{
// m_Rigidbody.isKinematic = false;
// Determine the number of degrees to be turned based on the input, speed and time between frames.
float turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime;
// Make this into a rotation in the y axis.
Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
// Apply this rotation to the rigidbody's rotation.
m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation);
/* else
{
m_Rigidbody.isKinematic = true;
}*/
}
}
`
1条答案
按热度按时间ulmd4ohb1#
当你设置
isKinematic
时,你告诉物理引擎不要对游戏对象施加力。然而,你的对象仍然有 * 速度 *,你需要自己取消它。
其他注意事项:
你使用物理引擎有什么原因吗?如果你想让它使用物理引擎,那么可以在
RigidBody
检查器中使用“拖动”来停止,或者在相反的方向施加一个力:如果你在做物理,你应该试着把
isKinematic
保持在false
。或者,* 不使用物理引擎 *,保持
isKinematic
设置为true
,并移动变换。