Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Enemy not colliding

$
0
0
So what I have here is enemy AI where the enemy chases the player when the player gets a certain distance from them and attacks when the player is close. While it is working how I want, the enemy is not colliding with anything. It currently has a capsule collider, which isn't helping. Adding a rigidbody doesn't help either. using UnityEngine; using System.Collections; public class EnemyBehavior : MonoBehaviour { public enum EnemyState{IDLE = 0, WALK = 1, RUN = 2, ATTACK = 3, KILL = 4}; public AnimationClip enemyIdle;//setting animations public AnimationClip enemyWalk;//walking public AnimationClip enemyRun;//running/chasing public AnimationClip enemyAttack;//attacking player public AnimationClip enemyKill;//killing player public Transform Player;//adds the player to the script private int Stopped = 0; public int WalkingSpeed = 1;//sets the normal walking speed public int RunningSpeed = 5;//sets the running (chase player) speed public int DefaultRunningSpeed = 5; public int MaxDist= 8;//maximum distance the player can be before enemy stops chasing the player public int MinDist= 1;//how close the player can be to enemy for enemy to start chasing player public EnemyState currentState; private bool PlayerDetected; void Update() { if(Vector3.Distance(transform.position,Player.position) <= MaxDist)//if that object has the tag "Player" { PlayerDetected = true; if (PlayerDetected) { print ("Player Detected."); transform.LookAt(Player);//Looks at the player currentState = EnemyState.RUN; transform.position += transform.forward*RunningSpeed*Time.deltaTime;//follows the player } }else if(Vector3.Distance(transform.position,Player.position) > MaxDist) { RoamAround (); } if (Vector3.Distance (transform.position,Player.position) < MinDist) { currentState = EnemyState.ATTACK; Attack (); } if (currentState == EnemyState.IDLE) { //print ("The enemy is idle."); animation.CrossFade (enemyIdle.name); } else if (currentState == EnemyState.WALK) { print ("The enemy is walking."); animation.CrossFade (enemyWalk.name); } else if (currentState == EnemyState.RUN) { print ("The enemy is running."); print ("Distance: " + Vector3.Distance(transform.position,Player.position)); animation.CrossFade (enemyRun.name); RunningSpeed = DefaultRunningSpeed; } else if (currentState == EnemyState.ATTACK) { print ("The enemy is attacking."); animation.CrossFade (enemyAttack.name); RunningSpeed = Stopped; } else if (currentState == EnemyState.KILL) {; print ("The enemy is killing."); animation.CrossFade (enemyKill.name); } } void RoamAround() { currentState = EnemyState.IDLE;//Add other RoamAround stuff here } public void Attack() { //Add attacking stuff here //Application.LoadLevel ("GameOver"); } } How could I fix this to where it still behaves the same but also collides with walls and has gravity? (Also I'd like it to also stop chasing the player if a wall is in the way but I haven't worked on that yet. My main problem here is collision.)

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>