Version: 2023.1

Rigidbody.MovePosition

切换到手册
public void MovePosition (Vector3 position);

参数

position Rigidbody 对象提供新位置。

描述

将运动 Rigidbodyposition 移动。

Rigidbody.MovePosition moves a Rigidbody and complies with the interpolation settings. When Rigidbody interpolation is enabled, Rigidbody.MovePosition creates a smooth transition between frames. Unity moves a Rigidbody in each FixedUpdate call. The position occurs in world space. Teleporting a Rigidbody from one position to another uses Rigidbody.position instead of 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, which is //multiplied by deltaTime and speed for a smooth MovePosition m_Rigidbody.MovePosition(transform.position + m_Input * Time.deltaTime * m_Speed); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961