Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Swiftly · Apr 22, 2016 at 02:43 PM · raycastingenemy ai

How to make AI avoid walls etc ?

Right now , my enemy ai can detect my player with range variable and follow and shoot me ...... i want my ai to stop at certain distance and then shoot and also go back to its orignal position if the Player is not in the range. and last thing is that ai can move through walls too how to avoid that ? its my first question here so please help ! Here is my code :

 using UnityEngine;
 using System.Collections;
 
 public class shootplayer : MonoBehaviour {
 
     private Transform player;
     public float attackDistance =10f;
     //public float chase =10f;
     public GameObject bullet;
     public GameObject BulletHole;
 
     public float delayTime = 8;
     private float counter = 0 ;
     public float enemyDamage=25f;
     // Use this for initialization
     void Start () {
         player = GameObject.Find ("FPSController").GetComponent<Transform> ();
 
     }
 
     // Update is called once per frame
     void Update () {
 
 
 
 
 
     if (Vector3.Distance (transform.position, player.position) < chase){
 
             transform.LookAt (player);
             transform.position = Vector3.MoveTowards (transform.position, player.position, 5 * Time.deltaTime);
         
     }
         if (Vector3.Distance (transform.position, player.position) < attackDistance) {
 
 
             Instantiate (bullet, transform.position, transform.rotation);
 
             AudioSource audio = GetComponent <AudioSource> ();
             audio.Play();
             counter = 0;
 
 
 
             RaycastHit hit;
             Ray ray = new Ray (transform.position, transform.forward);
             if (Physics.Raycast (ray, out hit, 100f))
             if (hit.transform.tag == "Player") {
 
 
                 hit.transform.GetComponent <HealthScript> ().RemoveHealth(enemyDamage);
             }
             else
             {
                 
 
 
             }
 
 
         }
     }
 
 
     //void OnTriggerEnter(Collider other)
     //{
 
     //if (other.GetComponent<Collider>().name == "FPSController") {
 
     //Destroy (gameObject);
 
     //}
     //}
 }
 
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

1 Reply

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

Answer by Soraphis · Apr 22, 2016 at 05:11 PM

  • avoid enemy can walk through walls:

your enemy should be an physical object, with collision and all that stuff.

  • make ai avoid walls:

when your ai decides to walk to a certain position, it should calculate a path, it can walk along. you can use the unity navmesh for this (if it is possible for your game) or you could implement some kind of A*-Algorithm.

  • AI Behaviour:

To model your AI behaviour you can use some kind of behavour-tree or decision-tree. if you've a real complex ai, or multiple different ai's you should consider a graphical representation of those trees.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Death Animation Playing On A Different Enemy I Shoot 1 Answer

Raycast never gets called 1 Answer

Raycasting an object with Oculus 0 Answers

Help with Raycast Script 0 Answers

Need help getting my door to open and close without triggering other doors. 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