Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by WesterlyCarrot9 · Mar 08, 2013 at 02:12 AM · c#enemymoveattackcircle

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;    
 }

} }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Mar 08, 2013 at 03:52 AM 0
Share

I don't see a definition for circleRadius. You can add a variable, or just put in a number.

avatar image WesterlyCarrot9 · Mar 08, 2013 at 05:35 AM 0
Share

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:

http://imageshack.us/photo/my-images/145/errorsp.jpg/

avatar image robertbu · Mar 08, 2013 at 07:10 AM 1
Share

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) {
         }
     }
 }
avatar image WesterlyCarrot9 · Mar 08, 2013 at 07:24 AM 0
Share

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

avatar image robertbu · Mar 08, 2013 at 03:53 PM 1
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Kiloblargh · Mar 08, 2013 at 06:52 AM

A "kind of invisible circle," you say... you mean, something like— *this?*

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image WesterlyCarrot9 · Mar 08, 2013 at 07:08 AM 0
Share

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 :)

avatar image Kiloblargh · Mar 08, 2013 at 07:17 AM 0
Share

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.

avatar image
0

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.....

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Maulik2208 · Mar 08, 2013 at 07:11 AM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges