Skip to content

Instantly share code, notes, and snippets.

@TheCuttlefish
Created March 25, 2021 15:54
Show Gist options
  • Save TheCuttlefish/9bd5332d29accee0322df91d907e833d to your computer and use it in GitHub Desktop.
Save TheCuttlefish/9bd5332d29accee0322df91d907e833d to your computer and use it in GitHub Desktop.
Simple agent movement using 2 triggers (left and right)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public GameObject leftCheck;
public GameObject rightCheck;
// Update is called once per frame
void Update()
{
transform.Translate(0, 0.01f, 0);
if(leftCheck.GetComponent<TriggerCheck>().isTriggered && rightCheck.GetComponent<TriggerCheck>().isTriggered)
{
transform.Rotate(0, 0, 180);
}
if (leftCheck.GetComponent<TriggerCheck>().isTriggered)
{
transform.Rotate(0, 0, -3);
}
if (rightCheck.GetComponent<TriggerCheck>().isTriggered)
{
transform.Rotate(0, 0, 3);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TriggerCheck : MonoBehaviour
{
public bool isTriggered;
private void OnTriggerStay2D(Collider2D collision)
{
if (collision.tag == "Interactable")
{
isTriggered = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.tag == "Interactable")
{
isTriggered = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment