Skip to content

Instantly share code, notes, and snippets.

@slonermike
Last active April 20, 2018 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slonermike/d0dc8103736358a31906b7b40bc93f54 to your computer and use it in GitHub Desktop.
Save slonermike/d0dc8103736358a31906b7b40bc93f54 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// To be added to an AI to control being pushed by outside sources.
/// </summary>
public class PushModule : MonoBehaviour {
[Tooltip("Rate at which we return to velocity zero.")]
public float damping = 2f;
[Tooltip("True to automatically apply the push velocity. False to allow some other system to handle that.")]
public bool autoApply = true;
Vector3 _velocity = Vector3.zero;
public Vector3 velocity {
get {
return _velocity;
}
}
// Update is called once per frame
void Update () {
_velocity = Vector3.Lerp(velocity, Vector3.zero, damping * Time.deltaTime);
if (_velocity.sqrMagnitude < 0.1f) {
_velocity = Vector3.zero;
}
if (autoApply) {
transform.position += velocity * Time.deltaTime;
}
}
public void AddVelocity(Vector3 additionalVel)
{
_velocity += additionalVel;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment