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

Enemy Chasing player?

$
0
0
I'm having an enemy move back and forward and then when he detects the player entering a cube that is parented to him I want him to face the player and move towards the player. This is going to make him go from walking to running hence why his speed is increased. When the player leaves the bound I want the enemy to return back to his original location and start moving forward and backward again. The problem is that when the player enters the enemy's space and he faces the player. For some reason he decides to freeze for a second or two then start moving towards the player at great speed then after awhile freeze again and then start moving towards the player again at great speeds. What am I missing, I should be using OnCollisionStay right? EDIT: I just noticed that I had to turn "one" and "two" bools off. I suppose I could ask instead how I would make him return to his original location. Would you suggest making the enemy a parent of a cube and move him back towards the cube and when he hits the cube make his rotation the same rotation as the cube and then turn the one bool on again? using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy : MonoBehaviour { Rigidbody enemyRigidBody; bool enemyIsMoving; public float enemySpeed = 2f; bool one; bool two; public float ToTarget; public static float distanceFromTarget; public Transform Player; void Start() { GetComponent(); GetComponent(); one = true; enemyIsMoving = true; StartCoroutine ("movementTime"); } void FixedUpdate() { if (one == true) { transform.Translate (Vector3.forward * enemySpeed * Time.deltaTime); } else if (two == true) { transform.Translate (Vector3.back * enemySpeed * Time.deltaTime); } } IEnumerator movementTime() { while (enemyIsMoving) { yield return new WaitForSeconds (5); one = false; two = true; yield return new WaitForSeconds (5); one = true; two = false; } } void OnTriggerStay(Collider thePlayer) { if (thePlayer.gameObject.tag == "Player") { Debug.Log ("1"); enemyIsMoving = false; ChasePlayer (); } } void OnTriggerExit(Collider thePlayer) { if (thePlayer.gameObject.tag == "Player") { enemyIsMoving = true; enemySpeed = 2; } } void ChasePlayer() { enemySpeed = 5; transform.LookAt (Player); transform.Translate (Vector3.forward * enemySpeed * Time.deltaTime); } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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