Question by
mcgamer16yt · Sep 23, 2017 at 01:28 PM ·
terraincolliderfalling-through-terrain
Enemy Walks through terrain how can I fix?
My enemy AI will walk towards the player, but each step he takes the Y coordinate goes down and he goes through the terrain to the point were half his body is under the terrain.
I have been stuck on this awhile here is the code I am using:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class AlienFollow : MonoBehaviour {
public GameObject ThePlayer;
public float TargetDistance;
public float AllowedRange = 10;
public GameObject TheEnemy;
public float EnemySpeed;
public int AttackTrigger;
public RaycastHit Shot;
void Update()
{
transform.LookAt(ThePlayer.transform);
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
{
TargetDistance = Shot.distance;
if (TargetDistance < AllowedRange)
{
EnemySpeed = 2f;
if (AttackTrigger == 0)
{
TheEnemy.GetComponent<Animation>().Play("AlienS_FM_Casual_walk");
transform.position = Vector3.MoveTowards(transform.position, ThePlayer.transform.position, EnemySpeed*Time.deltaTime);
}
}
else
{
EnemySpeed = 0;
TheEnemy.GetComponent<Animation>().Play("AlienS_F_IDLE");
}
}
if (AttackTrigger == 1)
{
EnemySpeed = 0;
TheEnemy.GetComponent<Animation>().Play("AlienS_F_Fire_standing");
}
}
void OnTriggerEnter()
{
AttackTrigger = 1;
}
void OnTriggerExit()
{
AttackTrigger = 0;
}
}
Comment