- Home /
How do I make my sprite go left or right when it hits a trigger?
I'm trying to make my character change directions when it hits a trigger at the end of the map but it isn't working. I've checked the bool through debug but its changes the way it should. using UnityEngine; using System.Collections;
public class bot : MonoBehaviour {
public float WalkSpeed;
private bool Direction;
void Start () {
Direction = true;
}
void Update () {
if (Direction = true){
botMovement1();
}
if (Direction = false){
botMovement2();
}
}
void botMovement1()
{
float AmtToMove = WalkSpeed * Time.deltaTime;
transform.Translate(Vector3.right * AmtToMove, Space.World);
}
void botMovement2()
{
float AmtToMove = WalkSpeed * Time.deltaTime;
transform.Translate(Vector3.left * AmtToMove, Space.World);
}
public void OnTriggerEnter2D(Collider2D otherObject)
{
if (otherObject.tag == "RightTrigger"){
Direction = false;
}
if (otherObject.tag == "LeftTrigger"){
Direction = true;
}
}
}
Comment