Skip to content

Instantly share code, notes, and snippets.

@Adjuvant
Created April 21, 2019 14:13
Show Gist options
  • Select an option

  • Save Adjuvant/9b4d9346a3cbdb27878dd722baa53b0e to your computer and use it in GitHub Desktop.

Select an option

Save Adjuvant/9b4d9346a3cbdb27878dd722baa53b0e to your computer and use it in GitHub Desktop.
Stop objects freaking out, update heavy though.
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