• 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
Question by NinjaEntertainment · Oct 07, 2017 at 07:21 PM · rigidbodyvector3aidistancecheck

Enemy AI help with height check

Hello there guys. Im making an AI and my problem is that i don't know how to check height between player and enemy.Enemy can attack player even if he is 20m above him. i don't know how to check that!! Im using Vector3 distance. Please help me if you can !! i would really appreciate it!!!!! My enemy script: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class EnemyRobot : MonoBehaviour {
     public GameObject gun;
     public Transform player;
     static Animator anim;
     public float attackTime;
     public float coolDown = 2f;
     public GameObject bullet;
     public Transform spawn;
 
         void Start () {
             anim = GetComponent<Animator> ();
         gun.SetActive (false);
         }
 
         // Update is called once per frame
     void Update () {
         Vector3 direction = player.position - this.transform.position;
         float angle = Vector3.Angle (direction, this.transform.forward);
         if (Vector3.Distance (player.position, this.transform.position) < 30 && angle < 180) {
 
             direction.y = 0;
 
             this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), 0.1f);
             anim.SetBool ("isIdle", false);
             if (direction.magnitude > 10) {
                     GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY;
                     this.transform.Translate (0, 0, 0.05f);
                     anim.SetBool ("isWalking", true);
                     anim.SetBool ("isAttacking", false);
                 gun.SetActive (false);
             }else {
                     if (attackTime > 0)
                         attackTime -= Time.deltaTime;
 
                     if (attackTime < 0)
                         attackTime = 0;
 
 
                 if (attackTime == 0) {
                         attackTime = coolDown;
                         GameObject ballInstance;
                         ballInstance = Instantiate (bullet, spawn.position, spawn.rotation) as GameObject;
                         ballInstance.GetComponent<Rigidbody> ().AddForce (transform.forward * 5000);
                         anim.SetBool ("isAttacking", true);
             
                         anim.SetBool ("isWalking", false);
                         gun.SetActive (true);
                     if (direction.magnitude < 3) {
                         GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.FreezeAll;
                     }
                 }
                 }
 
 
             } else {
                 anim.SetBool ("isIdle", true);
                 anim.SetBool ("isWalking", false);
                 anim.SetBool ("isAttacking", false);
             gun.SetActive (false);
 
             }
         }        
     }
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by NinjaEntertainment · Oct 07, 2017 at 08:33 PM

GUYS I MADE IT!! HERE IS THE SCRIPT NOW: using UnityEngine; using System.Collections; using System;

 public class EnemyRobot : MonoBehaviour {
     public GameObject gun;
     public Transform player;
     static Animator anim;
     public float attackTime;
     public float coolDown = 2f;
     public GameObject bullet;
     public Transform spawn;
 
     void Start () {
         anim = GetComponent<Animator> ();
         gun.SetActive (false);
     }
 
     // Update is called once per frame
     void Update () {
         if (Mathf.Abs (this.transform.position.y - player.transform.position.y) < 2f) { // He is above or below}
             Vector3 direction = player.position - this.transform.position;
             float angle = Vector3.Angle (direction, this.transform.forward);
             if (Vector3.Distance (player.position, this.transform.position) < 30 && angle < 180) {
 
                 direction.y = 0;
 
                 this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), 0.1f);
                 anim.SetBool ("isIdle", false);
                 if (direction.magnitude > 10) {
                     GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY;
                     this.transform.Translate (0, 0, 0.05f);
                     anim.SetBool ("isWalking", true);
                     anim.SetBool ("isAttacking", false);
                     gun.SetActive (false);
                 } else {
                     if (attackTime > 0)
                         attackTime -= Time.deltaTime;
 
                     if (attackTime < 0)
                         attackTime = 0;
 
 
                     if (attackTime == 0) {
                         attackTime = coolDown;
                         GameObject ballInstance;
                         ballInstance = Instantiate (bullet, spawn.position, spawn.rotation) as GameObject;
                         ballInstance.GetComponent<Rigidbody> ().AddForce (transform.forward * 5000);
                         anim.SetBool ("isAttacking", true);
 
                         anim.SetBool ("isWalking", false);
                         gun.SetActive (true);
                         if (direction.magnitude < 10) {
                             GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.FreezeAll;
                         }
                     }
                 }
 
 
             } else {
                 GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.FreezeAll;
                 anim.SetBool ("isIdle", true);
                 anim.SetBool ("isWalking", false);
                 anim.SetBool ("isAttacking", false);
                 gun.SetActive (false);
 
             }
         }  
     }
 }

Im checking height before distance... :DDD ALL THANKS TO fafase

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

144 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Check if object is moving towards a point 1 Answer

Make Navmesh agent follow closest object with tag 0 Answers

Check distance between many objects 3 Answers

Vector3.Distance edge of model 3 Answers

Detecting the right time to play a landing animation? 1 Answer


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