27 lines
833 B
C#
27 lines
833 B
C#
using UnityEngine;
|
|||
|
|
using System.Collections;
|
||
|
|
|
||
|
|
public static class AdvancedPhysics {
|
||
|
|
|
||
|
|
public static void AddTorqueAtPosition(this Rigidbody body, Vector3 torque, Vector3 position, ref Vector3 force, ref Vector3 ortho){
|
||
|
|
Vector3 torqueAxis = torque.normalized;
|
||
|
|
ortho = new Vector3(1,0,0);
|
||
|
|
|
||
|
|
if ( (torqueAxis-ortho).sqrMagnitude < 0.01f ) {
|
||
|
|
ortho = new Vector3(0,1,0);
|
||
|
|
}
|
||
|
|
|
||
|
|
Vector3.OrthoNormalize(ref torqueAxis, ref ortho);
|
||
|
|
force = Vector3.Cross(0.5f * torque, ortho);
|
||
|
|
|
||
|
|
body.AddForceAtPosition (force, position + ortho);
|
||
|
|
body.AddForceAtPosition (-force, position - ortho);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void AddTorqueAtPosition(this Rigidbody body, Vector3 torque, Vector3 position){
|
||
|
|
Vector3 force = new Vector3 ();
|
||
|
|
Vector3 ortho = new Vector3 ();
|
||
|
|
AddTorqueAtPosition (body, torque, position, ref force, ref ortho);
|
||
|
|
}
|
||
|
|
}
|