Rigidbody.position/rotation更新测试

本文探讨了Unity中Rigidbody组件的position/rotation属性如何在物理步前进行更新,并通过实验验证了其更新机制。发现这些属性的更改不会立即反映在transform上,而是在下一个物理步执行时才生效。

Rigidbody.position/rotation主要提供在下一个物理步之前更新物理位置,一般用于SweepTest这样的接口

 

那么测试一下会不会修改掉transform.position的值

 

测试结果:并不会立即改变transform.position,而会在下一个物理步执行时改变。如果手动改变transform.position/rotation不受影响

 

测试脚本:

using UnityEngine;

public class RigidbodyStepTest : MonoBehaviour
{
    public Rigidbody rigidbodyComponent;


    void OnEnable()
    {
        rigidbodyComponent.position = Vector3.zero;
        Debug.Log("OnEnable: " + transform.position + " frame count: " + Time.frameCount);
    }

    void FixedUpdate()
    {
        Debug.Log("FixedUpdate: " + transform.position + " frame count: " + Time.frameCount);
    }

    void Update()
    {
        Debug.Log("Update: " + transform.position + " frame count: " + Time.frameCount);
    }
}

 

 

结果:

 

 

由于Unity物理的更新在FixedUpdate之后,而FixedUpdate调用在Update之前,故Update时值归0了

转载于:https://www.cnblogs.com/hont/p/5217800.html

using System.Collections; using System.Collections.Generic; using UnityEngine; using static UnityEngine.GraphicsBuffer; public class 剑气 : MonoBehaviour { public Transform Center; private Rigidbody2D rb; [Tooltip("旋转")] public float 初始角度 = 45f; // 新增:初始角度(度) private float 当前角弧度; // 当前角度(弧度) public float angularSpeed = 1.0f; // 角速度(弧度/秒) private bool Temp1 = false; private bool Temp2 = false; // Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody2D>(); 当前角弧度 = 初始角度 * Mathf.Deg2Rad; // 将初始角度从度转换为弧度 } // Update is called once per frame void Update() { float errorMargin = 0.01f; // 定义一个小的误差范围 if (当前角弧度 <= -Mathf.PI / 4 + errorMargin) { 顺时针(); } else { Temp1 = true; } if (Temp1 == true && Temp2 == false) { 逆时针(); if (当前角弧度 >= Mathf.PI / 4 - errorMargin) { Temp2 = true; } } if (Temp1 == true && Temp2 == true) { //Destroy(gameObject); Debug.Log("Destroy"); } } void 顺时针() { 当前角弧度 -= angularSpeed * Time.deltaTime; // 更新当前角弧度 float x = Center.position.x + 0.01f * Mathf.Cos(当前角弧度); float y = Center.position.y + 0.01f * Mathf.Sin(当前角弧度); transform.position = new Vector3(x, y, transform.position.z); } void 逆时针() { 当前角弧度 += angularSpeed * Time.deltaTime; // 更新当前角弧度 float x = Center.position.x + 0.01f * Mathf.Cos(当前角弧度); float y = Center.position.y + 0.01f * Mathf.Sin(当前角弧度); transform.position = new Vector3(x, y, transform.position.z); } } unity如何记录物体运动轨迹并让另一个物体循迹运动
最新发布
09-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值