Skip to content

Instantly share code, notes, and snippets.

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

Embed
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 = 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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.