Right now my GameObject (enemy) is moved towards the CharacterController (player) on each update in the C# script. I am using:
transform.position = Vector3.MoveTowards(transform.position,character.transform.position, 0.03f);
This works, but my problem is that the update method is ignoring collision and causing the enemy to move inside of my player. Instead i want it to collide with the player, but not move inside of it.
I’d put that in a IF statement and set a flag for when it collides with the player in OnCollisionEnter
Something like ( Disclaimer: i have no idea if this code will run, just writing off the top of my head ):
bool hitCharacter = false;
GameObject character;
void Start(){
character = GameObject.FindObjectWithTag("Player"); //Get the player/target
}
void Update(){
if(!hitCharacter) //check to see if character is hit
transform.position = Vector3.MoveTowards(transform.position,character.transform.position, 0.03f); //move towards character
}
void OnCollisionEnter (Collider other){
if(other == character)
hitCharacter = true;
}
void OnCollisionStay (Collider other){
//do something hit-y or damage-y
}
void OnCollisionExit (Collider other){
if(other == character)
hitCharacter = false;
}