Unity 刚体其它一些常用方法和属性

Unity刚体除了AddForce、AddTorque、AddRelativeForce、AddForceAtPosition、AddExplosionForce、AddForceAtPosition方法,还有其它一些常见的方法和属性:

(1)Rigidbody.MovePosition(Vector3 position)

该方法用于移动刚体到某个位置。如:

    public float speed = 10f;
    private Rigidbody rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    private void FixedUpdate()
    {
        Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
        Vector3 newPosition = rb.position + movement * speed * Time.fixedDeltaTime;

        rb.MovePosition(newPosition);
    }

(2)Rigidbody.MoveRotation(Quaternion rot)

该方法用于旋转刚体,如:

    public float rotationSpeed = 100f;
    private Rigidbody rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    private void FixedUpdate()
    {
        Quaternion rotation = Quaternion.Euler(0f, Input.GetAxis("Horizontal") * rotationSpeed * Time.fixedDeltaTime, 0f);
        Quaternion newRotation = rb.rotation * rotation;

        rb.MoveRotation(newRotation);
    }

(3)Rigidbody.GetRelativePointVelocity(Vector3 relativePoint)

该方法用于获取刚体上某一点的相对速度,它需要传入一个相对于刚体本地坐标系的点作为参数,结果返回该点的相对速度向量,如:

    public Transform targetPoint;
    private Rigidbody rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        Vector3 relativeVelocity = rb.GetRelativePointVelocity(targetPoint.position);
        Debug.Log(relativeVelocity);
    }

(4)Rigidbody.GetPointVelocity(Vector3 worldPoint)

该方法与上面方法类似,也是用于获取刚体上某一点的速度,区别就是特定点是相对于世界坐标系的。

(5)Rigidbody.velocity

该属性为刚体的线性速度。使用该属性我们既可以获取刚体的线性速度,也可以设置刚体的线性速度。如:

        // 获取刚体的线性速度
        Vector3 currentVelocity = rb.velocity;
        Debug.Log("当前速度: " + currentVelocity);

        // 设置刚体的线性速度
        Vector3 newVelocity = new Vector3(10f, 0f, 0f);
        rb.velocity = newVelocity;

(6)Rigidbody.Sleep()

该方法可以使刚体进入休眠状态,处于该状态的刚体将不再响应碰撞或受力,并且不会更新其位置和旋转。使用该方法有利于介绍计算资源。

(7)Rigidbody.IsSleeping()

该属性用于检查刚体是否处于休眠状态。这个属性是只读的,可以用于判断刚体当前是否处于休眠状态。

(8)Rigidbody.WakeUp()

该方法用于唤醒刚体,将其从休眠状态中唤醒。

  • 13
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不在同一频道上的呆子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值