public void MovePosition (Vector3 position);

参数

positionRigidbody 对象提供新位置。

描述

将运动 Rigidbodyposition 移动。

Rigidbody.MovePosition 会移动刚体,并遵循插值设置。启用了刚体插值时,Rigidbody.MovePosition 会在各帧之间创建平滑过渡。Unity 在每个 FixedUpdate 调用中移动 Rigidbodyposition 出现在本地空间中。将 Rigidbody 从一个位置传送到另一个位置会使用 Rigidbody.position 而不是 MovePosition

using UnityEngine;

public class Example : MonoBehaviour { 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, //multiplied by time and speed for a smooth MovePosition m_Rigidbody.MovePosition(transform.position + m_Input * Time.deltaTime * m_Speed); } }