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 Tyson1211 · Sep 26, 2015 at 04:44 PM · c#aienemyaistoppingghosting

Enemy AI Issues!!?!

My AI is being stubborn and I was wondering if their is a quick fix to my problem... I have a box colliders on everything from walls to player to AI, but for some reason my Enemy wants to faze through walls, and keeps pushing my character when it's shouldn't... i was wondering if their is a quick fix to this problem I provided the script and a picture of the problem... "NO Error in script"

  • want my enemy to stop at a distance of .25 away from player... I want my enemy to avoid and not go through walls...

    using UnityEngine; using System.Collections;

    [RequireComponent(typeof(SphereCollider))] public class EnemyAI : MonoBehaviour {

       public float preceptionRadius = 10;
     
         public Transform target;
         public float movementSpeed = 5;
         public float rotationSpeed = 420;
     
         private Transform myTransform;
     
         // Use this for initialization
         void Start ()
         {
             SphereCollider sc = GetComponent<SphereCollider>();
             CharacterController cc = GetComponent<CharacterController>();
     
             if (sc == null)
                 Debug.LogError("No Collider on enemy");
             else
             {
                 sc.isTrigger = true;
             }
     
             if(cc == null)
             {
                 Debug.LogError("There's no Character Controller");
             }
             else
             {
                 sc.center = cc.center;
                 sc.radius = preceptionRadius;
             }
     
             myTransform = transform;
         }
     
         // Update is called once per frame
         void Update()
         {
             if (target)
             {
                 Vector3 dir = (target.position - myTransform.position).normalized;
                 float direction = Vector3.Dot(dir, transform.forward);
     
                 float dist = Vector3.Distance(target.position, myTransform.position);
     
                 //Find or Look at Target
                 myTransform.rotation = Quaternion.Lerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
     
                 //Movement
                 myTransform.position += myTransform.forward * movementSpeed * Time.deltaTime;
             }
         }
     
         public void OnTriggerEnter(Collider other)
         {
             Debug.Log("Entered");
             if (other.CompareTag("Player"))
             {
                 target = other.transform;
             }
         }
         public void OnTriggerExit(Collider other)
         {
             Debug.Log("Exit");
             if(other.CompareTag("Player"))
             {
                 target = null;
             }
         }
     }
     
    
    
    

alt text

error.png (234.3 kB)
untitled.png (190.3 kB)
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

2 Replies

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

Answer by lloladin · Sep 26, 2015 at 05:41 PM

to make the enemy stop moving when he hits the player i would use line cast and then make a bool moveTorwardsPlayer and set the bool to true if the player is not in the line cast and the same for the wall just if walls are not in the line cast

some Documentation [http://docs.unity3d.com/ScriptReference/Physics.Linecast.html][1]

Example

 public bool moveTorwardsPlayer;
 public LayerMask MoveBlockers;
 
 public Void FixedUpdate()
 {
    moveTorwardsPlayer = Physic2D.linecast("FirstPoint.poistion","SecoundPoint", MoveBlockers );
 }
 
 public Void Update()
 {
    if (moveTorwardsPlayer)
 {
    // Do ai stuff
 }
 }

this is untested code but it should give you the idea of what to do hope this helps :)

Comment
Add comment · Show 11 · 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 Tyson1211 · Sep 26, 2015 at 06:16 PM 0
Share

Wait so what you are saying is that if i add this to my script i could apply an if statement into another if statement? ALso can I change the Phisics2D to 3d since its in a 3d field and not 2d,its a 3d game...

avatar image lloladin · Sep 26, 2015 at 07:40 PM 0
Share

yes you can change it to 3d just write Physic.Linecast and fill in the parrmeters and you simple Wrap the if (moveTorwardsPlayer) Around the if (Target) statement

avatar image Tyson1211 lloladin · Sep 30, 2015 at 07:05 PM 0
Share

I did what you said as seen here... is it because I added a copy of target.position, myTransform.position?

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(SphereCollider))]
 public class EnemyAI : $$anonymous$$onoBehaviour {
 
     public float preceptionRadius = 10;
 
     public Transform target;
     public float movementSpeed = 5;
     public float rotationSpeed = 420;
 
     public bool moveTorwardsPlayer;
     public Layer$$anonymous$$ask $$anonymous$$oveBlockers;
 
     private Transform myTransform;
 
     // Use this for initialization
     void Start ()
     {
         SphereCollider sc = GetComponent<SphereCollider>();
         CharacterController cc = GetComponent<CharacterController>();
 
         if (sc == null)
             Debug.LogError("No Collider on enemy");
         else
         {
             sc.isTrigger = true;
         }
 
         if(cc == null)
         {
             Debug.LogError("There's no Character Controller");
         }
         else
         {
             sc.center = cc.center;
             sc.radius = preceptionRadius;
         }
 
         myTransform = transform;
     }
 
     public void FixedUpdate()
     {
         moveTorwardsPlayer = Physics.Linecast(target.position, myTransform.position, $$anonymous$$oveBlockers);
     }
 
     // Update is called once per frame
     void Update()
     {
         if (moveTorwardsPlayer)
         {
             if (target)
             {
                 Vector3 dir = (target.position - myTransform.position).normalized;
                 float direction = Vector3.Dot(dir, transform.forward);
 
                 float dist = Vector3.Distance(target.position, myTransform.position);
 
                 //Find or Look at Target
                 myTransform.rotation = Quaternion.Lerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
 
                 //$$anonymous$$ovement
                 myTransform.position += myTransform.forward * movementSpeed * Time.deltaTime;
             }
         }
     }
 
     public void OnTriggerEnter(Collider other)
     {
         Debug.Log("Entered");
         if (other.CompareTag("Player"))
         {
             target = other.transform;
         }
     }
     public void OnTriggerExit(Collider other)
     {
         Debug.Log("Exit");
         if(other.CompareTag("Player"))
         {
             target = null;
         }
     }
 }
 
avatar image lloladin Tyson1211 · Sep 30, 2015 at 08:15 PM 0
Share

hm i seem to have made a mistake the bool moveTorwardsPlayer should be dont$$anonymous$$oveTorwardsPlayer or some better name and the condition should be !dont$$anonymous$$oveTorwardsPlayer but also thats not quite how linecasting works you Draw a line between 2 Objects and then if anything touches that the bool is true else its false what you did is $$anonymous$$oveTorwards player aslong as player is inbetween Target and $$anonymous$$yTransForm so you should think about it like this

dont$$anonymous$$oveTorwardsPlayer = Physics.Linecast(Transform A, transform B, $$anonymous$$oveBlockers);

so 1 way to use it would be to add 2 Child Objects to youre Enemy TransForm a and Transform B and put them infront of the enemy so that when he turns torwards the player they will follow Documentation if you wana read about it :D http://docs.unity3d.com/ScriptReference/Physics.Linecast.html hope it helps :)

Show more comments
avatar image
0

Answer by Tyson1211 · Sep 26, 2015 at 04:46 PM

Enemy is black player is red...

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

32 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

Related Questions

Enemy watches player and shoots but bullets will not project.. 0 Answers

Change Enemy Direction 1 Answer

How do I make an AI(cube) that follows and rams the player? 0 Answers

A little doubt about creating a Talking BOT that simulates AI, for iOS system using Unity. 0 Answers

2D Enemy Ai 0 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