Rotating Enemy when enters trigger area
I am trying to make an enemy rotate 180 degrees when it enters a trigger area, I know how to do the rotation but I can't quite get the right code for the trigger. Have spent hours trying but still no joy. I have rigidbody set up and Is Trigger etc
Comment
BTW ideally I'd like to use tags for the triggers as I have Enemy and Trigger tags and the 180 degree turn is generic for all enemies so a one size fits all script is needed
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trigger : $$anonymous$$onoBehaviour
{
void OnTriggerEnter(Collider col) {
if(col.gameObject.tag == "Enemy");
{
transform.RotateAround (transform.position, transform.up, 180f);
}
}
}
Tried this but doesn't work, I'm not even sure if the transform is even reached
Answer by Ramus1973 · May 29, 2018 at 02:06 AM
Never mind, I got it to work myself
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trigger : MonoBehaviour
{
void OnTriggerEnter(Collider col)
{
if(col.gameObject.tag == "Enemy")
col.transform.RotateAround (transform.position, transform.up, 180f);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612163605im_/https://answers.unity.com/themes/thub/images/avi.jpg)