Skip to content

Instantly share code, notes, and snippets.

@slonermike slonermike/PushModule.cs
Last active Apr 20, 2018

What would you like to do?
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 =;
public Vector3 velocity {
get {
return _velocity;
// Update is called once per frame
void Update () {
_velocity = Vector3.Lerp(velocity,, damping * Time.deltaTime);
if (_velocity.sqrMagnitude < 0.1f) {
_velocity =;
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
You can’t perform that action at this time.