物体旋转太慢

Object is rotating too slowly

所以基本上我“制作”了一个控制手部运动的脚本,并且也有适当的碰撞。它的大部分工作,然而,旋转似乎有一个有限的速度,即使它的速度设置为高值。

旋转的速度很慢,不管我把乘数改成什么。

代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hand: MonoBehaviour
{
    // Physics Movement
    [SerializeField] private GameObject followObject;
    [SerializeField] private float followSpeed = 30f;
    [SerializeField] private float rotateSpeed = 100f;
    [SerializeField] private float teleportDistance = 0.75f;
    [SerializeField] private Vector3 positionOffset;
    [SerializeField] private Vector3 rotationOffset;
    private Transform _followTarget;
    private Rigidbody _body;
    private bool _inGrab = false;

    void Start()
    {
        // Physics Movement
        _followTarget = followObject.transform;
        _body = GetComponent<Rigidbody>();
        _body.collisionDetectionMode = CollisionDetectionMode.Continuous;
        _body.interpolation = RigidbodyInterpolation.Interpolate;
        _body.mass = 20f;

        // Teleport Hands
        _body.position = _followTarget.position;
        _body.rotation = _followTarget.rotation;
    }

    void Update()
    {
        PhysicsMove();
    }

    private void PhysicsMove()
    {
        // Position
        var positionWithOffset = _followTarget.position + positionOffset;
        var distance = Vector3.Distance(positionWithOffset, transform.position);
        _body.velocity = (positionWithOffset - transform.position).normalized * (followSpeed * distance);

        // Rotation
        var rotationWithOffset = _followTarget.rotation * Quaternion.Euler(rotationOffset);
        var q = rotationWithOffset * Quaternion.Inverse(_body.rotation);
        q.ToAngleAxis(out float angle, out Vector3 axis);
        _body.angularVelocity = axis * (angle * Mathf.Deg2Rad * rotateSpeed);

        // Teleport back when too far
        if(distance > teleportDistance)
        {
            _body.position = _followTarget.position;
            _body.rotation = _followTarget.rotation;
        }
    }
}

脚本附加到刚体手部模型,跟随目标为XR Controller。一切似乎都正常,除了旋转速度。

很少使用四元数,所以这可能只是一些基本问题。

您需要更改刚体的maxAngularVelocity。默认值仅为每秒 7 弧度,小于每秒 2 圈。

将其设置为更高的值(例如 20)将使刚体在需要时转动得更快。不幸的是,这无法通过检查器访问,但可以通过脚本访问:

void Start()
{
    // Physics Movement
    _followTarget = followObject.transform;
    _body = GetComponent<Rigidbody>();
    _body.collisionDetectionMode = CollisionDetectionMode.Continuous;
    _body.interpolation = RigidbodyInterpolation.Interpolate;
    _body.mass = 20f;
    _body.maxAngularVelocity = 20f;

    // Teleport Hands
    _body.position = _followTarget.position;
    _body.rotation = _followTarget.rotation;
}