所以基本上我"made"一个脚本,控制手的运动,并有适当的碰撞。它的大部分工作,然而,旋转似乎有一个有限的速度,即使它的速度被设置为很高的值。
无论我将乘数改为什么,旋转的速度都是缓慢的。
代码如下:
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控制器。一切似乎都在工作,除了旋转的速度。
很少处理四元数,所以它可能只是一些基本的问题。
您需要更改刚体的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;
}