- Home /
A way to create an invisible circle for the enemy?
Good evening everyone! I was wondering if there is a way to create a kind of invisible circle around my enemy cube so that when the player gets in, the enemy aggros him and moves and attacks him. But when the player leaves the circle area, the enemy should go on following him and attacking.These are the AI and Attack scripts i use for my enemy:
using UnityEngine; using System.Collections;
public class EnemyAttack : MonoBehaviour { public GameObject target; public float attackTimer; public float coolDown;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 2.0f;
}
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(attackTimer == 0){
Attack();
attackTimer = coolDown;
}
}
private void Attack(){
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
Debug.Log (distance);
if(distance < 2.5f) {
if(direction > 0) {
PlayerHealth eh = (PlayerHealth)target.GetComponent("PlayerHealth");
eh.AdjustCurrentHealth(-10);
}
}
} }
using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour { public Transform target; public int moveSpeed; public int rotationSpeed; public int maxDistance;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
maxDistance = 2;
}
// Update is called once per frame
void Update () {
Debug.DrawLine(target.position, myTransform.position, Color.yellow);
//Look at target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
//Move towards target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
} }
Answer by WesterlyCarrot9 · Mar 08, 2013 at 03:37 AM
Ok so i wrote what you said as you can see:
using System.Collections;
public class EnemyAI : MonoBehaviour { public Transform target; public int moveSpeed; public int rotationSpeed; public int maxDistance; public GameObject goPlayer; public GameObject goEnemy;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
goPlayer = GameObject.Find("Player");
goEnemy = GameObject.Find("Enemy");
maxDistance = 2;
}
// Update is called once per frame
void Update () {
Debug.DrawLine(target.position, myTransform.position, Color.yellow);
//Look at target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if (Vector3.Distance(goPlayer.transform.position, goEnemy.transform.distance) < circleRadius) {
// Do whatever you want when an enemy is inside the circle
}
if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
//Move towards target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
} }
But i get these 3 errors: There is no definition for "distance" and 2 of them are for the Vector3. did i do something wrong? :S
I don't see a definition for circleRadius. You can add a variable, or just put in a number.
Ok so i wrote it again like the one i showed you above and i also put a public int for circleRadius. However, the same errors keep popping up :( I dared to change the word distance to position in the code and then all errors were gone. I put a number in the radius but the cube still moves towards me and nothing happens. Here is a screenshot of them:
It should have been transform.position, not transform.distance. Here is the script so that it compiles:
using UnityEngine;
using System.Collections;
public class Bug02 : $$anonymous$$onoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public int maxDistance;
public GameObject goPlayer;
public GameObject goEnemy;
public float circleRadius = 2.0f;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
goPlayer = GameObject.Find("Player");
goEnemy = GameObject.Find("Enemy");
maxDistance = 2;
}
// Update is called once per frame
void Update () {
Debug.DrawLine(target.position, myTransform.position, Color.yellow);
//Look at target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if (Vector3.Distance(goPlayer.transform.position, goEnemy.transform.position) < circleRadius) {
}
}
}
Wow thanks for writing that!!But now although it works it seems that he just stands there looking at me and when i get like really close to him,he attacks. I need him to move towards me as well.Also, if i go too far he should stop looking at me. Is that possible? P.S. That line i have here which makes him stop at a certain distance is also something i don't want it gone :P
All these things are possible, but I'm not going to write them for you. Take honest try and implement each one, and then post the code back as a new as a new question if you cannot get it to work. $$anonymous$$eep the questions to one issue per post.
As a starting point, I don't see anything here that causes the enemy to look at the player. Look in the reference for transform.LookAt() and see if you can make him look at the player. The code changes I suggested calculates a distance. You can use something similar to turn the LookAt() on an off based on distance.
Answer by robertbu · Mar 08, 2013 at 02:45 AM
An easy way would be to check the distance between the center of the "enemy cube" and the player. The code below assumes you have your player name "Player" and your enemy cube namde "EnemyCube". Change the names to fit the names in your game. So at the top of the file you would put:
public GameObject goPlayer;
public GameObject goEnemyCube;
In Start() you would put:
goPlayer = GameObject.Find("Player");
goEnemyCube = GameObject.Find("EnemyCube");
The in your code in update where you want to decide between two behaviors:
if (Vector3.Distance(goPlayer.transform.position, goEnemyCube.transform.distance) < circleRadius) {
// Do whatever you want when an enemy is inside the circle
}
'circleRadius' will be the outside distance from the center of the enemy cube for the circle.
Answer by Kiloblargh · Mar 08, 2013 at 06:52 AM
A "kind of invisible circle," you say... you mean, something like— *this?*
What i want is yeah an invisible circle which will be the agro range of the enemy you know and as soon as the player gets in the enemy moves towards him in order to attack :)
I thought a sphere collider and OnTriggerEnter() was the obvious and standard way to do that sort of thing. Do some tutorials... I've been too busy to even look at Angry Bots but I'm assu$$anonymous$$g it does what you're trying to do and that's how it does it.
Answer by Maulik2208 · Mar 08, 2013 at 07:05 AM
SphereCast is the thing which suits you best for this..... for more info please follow the link -----> SphereCast Documentation
Don't Forget to mark the answer if found useful.... enjoy....Cheers.....
@WesterlyCarrot9 you need something like Field Runner or tower defense kind of stuff then the Physics.Spherecast is the thing for you so try it and enjoy
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Attacking more than one enemy with same tag, but unity only allows one enemy at a time? 2 Answers
Problem with enemy AI 1 Answer
Enemy move error 2 Answers