Created
March 25, 2021 15:54
-
-
Save TheCuttlefish/9bd5332d29accee0322df91d907e833d to your computer and use it in GitHub Desktop.
Simple agent movement using 2 triggers (left and right)
This file contains hidden or 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; | |
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); | |
} | |
} | |
} |
This file contains hidden or 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; | |
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