12using System.Collections;
13using System.Collections.Generic;
32 [AddComponentMenu(
"")]
35 private Rigidbody _body;
49 private float _mass = 10F;
51 private Vector3 _velocity;
53 get {
return _velocity; }
54 set { _velocity = value; }
58 get {
return Quaternion.Inverse(this.transform.rotation) * _velocity; }
61 private Vector3 _angularVelocity;
63 get {
return _angularVelocity; }
64 set { _angularVelocity = value; }
68 get {
return Quaternion.Inverse(this.transform.rotation) * _angularVelocity; }
71 private Vector3 _accumulatedForce;
72 private Vector3 _accumulatedTorque;
75 _body = GetComponent<Rigidbody>();
77 _body.isKinematic =
true;
92 private void updateShipPhysics() {
94 Vector3 acceleration = _accumulatedForce / _mass;
95 _velocity += acceleration * Time.deltaTime;
97 _accumulatedForce = Vector3.zero;
100 Vector3 newPosition = this.transform.position + _velocity * Time.deltaTime;
101 this.transform.position = newPosition;
105 Vector3 eulerAcceleration = _accumulatedTorque;
106 _angularVelocity += eulerAcceleration * Time.deltaTime;
108 _accumulatedTorque = Vector3.zero;
111 Quaternion newRotation = Quaternion.Euler(_angularVelocity * Time.deltaTime) * this.transform.rotation;
112 this.transform.rotation = newRotation;
116#if UNITY_2017_3_OR_NEWER
121 #region Ship Forces API
124 _accumulatedForce += force;
128 Vector3 toCenterOfMass = this.transform.TransformPoint(_body.centerOfMass) - position;
130 float forceCMAngle = Vector3.Angle(force, toCenterOfMass);
133 Vector3 linForce = force * Mathf.Cos(forceCMAngle);
134 _accumulatedForce += linForce;
137 Vector3 torqueVector = force * Mathf.Sin(forceCMAngle) * toCenterOfMass.magnitude;
139 _accumulatedTorque += Vector3.Cross(torqueVector, toCenterOfMass);
143 _accumulatedTorque += this.transform.rotation * shipAlignedTorque;
147 _accumulatedForce += this.transform.rotation * shipAlignedForce;
The spaceship in this example is a kinematic rigidbody with a force API, but having a rigidbody on yo...
void AddShipAlignedForce(Vector3 shipAlignedForce)
Rigidbody rigidbody
The ship contains Colliders, so it is given a kinematic Rigidbody to prevent the overhead of moving C...
void AddForceAtPosition(Vector3 force, Vector3 position)
Vector3 shipAlignedAngularVelocity
void AddForce(Vector3 force)
void AddShipAlignedTorque(Vector3 shipAlignedTorque)
Vector3 shipAlignedVelocity
Action OnPrePhysicalUpdate
static InteractionManager instance
Often, only one InteractionManager is necessary per Unity scene. This property will contain that Inte...