Unity - Scripting API: Rigidbody.MovePosition
Sat Jun 22 2024 16:01:43 GMT+0000 (Coordinated Universal Time)
Saved by
@gil
Rigidbody m_Rigidbody;
public float m_Speed = 5f;
void Start()
{
//Fetch the Rigidbody from the GameObject with this script attached
m_Rigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
//Store user input as a movement vector
Vector3 m_Input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
//Apply the movement vector to the current position, which is
//multiplied by deltaTime and speed for a smooth MovePosition
m_Rigidbody.MovePosition(transform.position + m_Input * Time.deltaTime * m_Speed);
}
content_copyCOPY
https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html
Comments