Created
April 21, 2019 14:13
-
-
Save Adjuvant/9b4d9346a3cbdb27878dd722baa53b0e to your computer and use it in GitHub Desktop.
Stop objects freaking out, update heavy though.
This file contains hidden or 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 LimitVelocity : MonoBehaviour | |
| { | |
| [SerializeField] | |
| protected float limit = Mathf.Infinity; | |
| [SerializeField] | |
| protected Vector3 weight = Vector3.one; | |
| protected Rigidbody rigidbody; | |
| protected void Start() | |
| { | |
| rigidbody = GetComponent<Rigidbody>(); | |
| if(rigidbody == null) | |
| { | |
| Debug.Log("Rigidbody is null in LimitVelocity, make sure a limited gameobject has a Rigidbody component."); | |
| } | |
| } | |
| protected void FixedUpdate () | |
| { | |
| if(rigidbody == null) return; | |
| float velocityMag = new Vector3(rigidbody.velocity.x * weight.x, rigidbody.velocity.y * weight.y, rigidbody.velocity.z * weight.z).magnitude; | |
| Vector3 veloictyNorm = rigidbody.velocity.normalized; | |
| if(velocityMag > limit) | |
| { | |
| rigidbody.velocity = veloictyNorm * limit ; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment