Skip to content

Instantly share code, notes, and snippets.

@oismaelash
Created October 2, 2022 20:15
Show Gist options
  • Save oismaelash/546945e6623964a4e4e67f494a668705 to your computer and use it in GitHub Desktop.
Save oismaelash/546945e6623964a4e4e67f494a668705 to your computer and use it in GitHub Desktop.
using UnityEngine;
namespace IsmaelAshAssets
{
[RequireComponent(typeof(Collider), typeof(Rigidbody))]
public class DashAutomatic : MonoBehaviour
{
public enum Direction { Left, Right, Forward, Back }
public Direction direction;
public int forceImpulse;
public string tagVerify;
private void Start()
{
if (!GetComponent<Collider>().isTrigger)
{
Debug.LogError($"Enable isTrigger on object = {name}");
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag(tagVerify))
{
Rigidbody player = other.GetComponent<Rigidbody>();
player.velocity = Vector3.zero;
switch (direction)
{
case Direction.Left:
player.AddForce(Vector3.left * forceImpulse, ForceMode.Impulse);
break;
case Direction.Right:
player.AddForce(Vector3.right * forceImpulse, ForceMode.Impulse);
break;
case Direction.Forward:
player.AddForce(Vector3.forward * forceImpulse, ForceMode.Impulse);
break;
case Direction.Back:
player.AddForce(Vector3.back * forceImpulse, ForceMode.Impulse);
break;
default:
break;
}
}
}
public void StopDashAutomatic(Rigidbody rigidbody)
{
rigidbody.velocity = Vector3.zero;
}
[ContextMenu("Test_StopDashAutomatic")]
private void Test_StopDashAutomatic()
{
Rigidbody rigidbody = FindObjectOfType<CapsuleCollider>().GetComponent<Rigidbody>();
StopDashAutomatic(rigidbody);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment