1using System.Collections;
2using System.Collections.Generic;
10 private new Rigidbody rigidbody;
12 private Vector3 targetPosition = Vector3.zero;
13 private Quaternion targetRotation = Quaternion.identity;
20 private float itemRadius = 0f;
21 private Bounds itemBounds;
22 private Renderer itemRenderer;
27 rigidbody = GetComponent<Rigidbody>();
28 rigidbody.maxAngularVelocity = 50;
31 public void MoveTo(Vector3 position, Quaternion rotation)
36 targetPosition = gameObject.transform.parent.TransformPoint(position);
37 targetRotation = rotation;
42 rigidbody.velocity = Vector3.zero;
43 rigidbody.angularVelocity = Vector3.zero;
45 rigidbody.MovePosition(position);
46 rigidbody.MoveRotation(rotation);
63 private void OnCollisionEnter(Collision collision)
69 var deltaPos = targetPosition - transform.position;
70 if (
float.IsNaN(deltaPos.x))
return;
74 Quaternion rotationDelta = targetRotation * Quaternion.Inverse(rigidbody.rotation);
78 rotationDelta.ToAngleAxis(out angle, out axis);
82 var deltaAngle = angle * axis;
83 if (
float.IsNaN(deltaAngle.x))
return;
84 if (rigidbody.velocity.magnitude < 1.5f)
86 rigidbody.velocity = Vector3.MoveTowards(rigidbody.velocity, targetPosition - transform.position, 2f)*4;
89 rigidbody.angularVelocity = Vector3.MoveTowards(rigidbody.angularVelocity, angle*axis, 2f);
92 bool itemCondition = Physics.CheckSphere(targetPosition, itemRadius,
collisionMask);
101 if (!handCollides && !itemCondition)
104 rigidbody.velocity = Vector3.zero;
105 rigidbody.angularVelocity = Vector3.zero;
113 rigidbody.MovePosition(targetPosition);
114 rigidbody.MoveRotation(targetRotation);
118 rigidbody.velocity = Vector3.zero;
119 rigidbody.angularVelocity = Vector3.zero;
121 Vector3 velocityTarget, angularTarget;
128 rigidbody.velocity = Vector3.MoveTowards(rigidbody.velocity, velocityTarget, maxVelocityChange);
129 rigidbody.angularVelocity = Vector3.MoveTowards(rigidbody.angularVelocity, angularTarget, maxAngularVelocityChange);
136 bool realNumbers =
false;
141 Vector3 positionDelta = (targetPosition - rigidbody.position);
142 velocityTarget = (positionDelta * velocityMagic * Time.deltaTime);
144 if (
float.IsNaN(velocityTarget.x) ==
false &&
float.IsInfinity(velocityTarget.x) ==
false)
149 velocityTarget = Vector3.zero;
152 Quaternion rotationDelta = targetRotation * Quaternion.Inverse(rigidbody.rotation);
157 rotationDelta.ToAngleAxis(out angle, out axis);
162 if (angle != 0 &&
float.IsNaN(axis.x) ==
false &&
float.IsInfinity(axis.x) ==
false)
164 angularTarget = angle * axis * angularVelocityMagic * Time.deltaTime;
169 angularTarget = Vector3.zero;
const float MaxAngularVelocityChange
bool GetTargetVelocities(out Vector3 velocityTarget, out Vector3 angularTarget)
void SetHoldItemBounds(Bounds b)
void ExecuteFixedUpdate()
const float AngularVelocityMagic
void MoveTo(Vector3 position, Quaternion rotation)
void SetHoldItemRadius(float r)
const float MaxVelocityChange
void SetHoldItemRenderer(Renderer r)