Last active
April 20, 2018 03:08
-
-
Save slonermike/d0dc8103736358a31906b7b40bc93f54 to your computer and use it in GitHub Desktop.
This file contains 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; | |
/// <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