-
-
Save anubisza/e763f75d19f1e84de952131b64254fda to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class RBMuscle : MonoBehaviour | |
{ | |
private const float TICK_EVERY_SECONDS = 1 / 25f; // 25 per second. | |
[Header("Setup")] | |
public bool _invert; | |
[Space] | |
public float _torqueForce; | |
public float _angularDamping; | |
public float _maxForce; | |
public float _springForce; | |
public float _springDaming; | |
[Space] | |
public Vector3 targetVel; | |
[Header("Bind")] | |
public Transform target; | |
private ConfigurableJoint _join; | |
private JointDrive _drive; | |
private SoftJointLimitSpring _spring; | |
private Quaternion _startingRotation; | |
private float _secondsSinceLastTick = 0; | |
private void Awake() { | |
_join = GetComponent<ConfigurableJoint>(); | |
} | |
void Start() { | |
_invert = false; | |
_torqueForce = 500f; | |
_angularDamping = 0.0f; | |
_maxForce = 500f; | |
_springForce = 0f; | |
_springDaming = 0f; | |
targetVel = new Vector3(0f, 0f, 0f); | |
_drive.positionSpring = _torqueForce; | |
_drive.positionDamper = _angularDamping; | |
_drive.maximumForce = _maxForce; | |
_spring.spring = _springForce; | |
_spring.damper = _springDaming; | |
_join.slerpDrive = _drive; | |
_join.linearLimitSpring = _spring; | |
_join.rotationDriveMode = RotationDriveMode.Slerp; | |
_join.projectionMode = JointProjectionMode.None; | |
_join.targetAngularVelocity = targetVel; | |
_join.enablePreprocessing = false; | |
_join.configuredInWorldSpace = false; | |
_join.swapBodies = true; | |
_join.xMotion = ConfigurableJointMotion.Locked; | |
_join.yMotion = ConfigurableJointMotion.Locked; | |
_join.zMotion = ConfigurableJointMotion.Locked; | |
_join.angularXMotion = ConfigurableJointMotion.Free; | |
_join.angularYMotion = ConfigurableJointMotion.Free; | |
_join.angularZMotion = ConfigurableJointMotion.Free; | |
_startingRotation = Quaternion.Inverse(target.localRotation); | |
} | |
private void Update() { | |
_secondsSinceLastTick += Time.deltaTime; | |
if (_secondsSinceLastTick < TICK_EVERY_SECONDS) return; | |
Tick(); | |
} | |
private void Tick() { | |
_secondsSinceLastTick = 0; | |
if (_invert) | |
_join.targetRotation = Quaternion.Inverse(target.localRotation * _startingRotation); | |
else | |
_join.targetRotation = target.localRotation * _startingRotation; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment