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
0
Question by SidusLevis · Jul 11, 2013 at 02:01 PM · physicsaienemyai

AI passes through walls and flies

I've made an AI that follows the player when within range, attacks when very close, and goes idle when out of range. The AI flies around to get to the player and can pass through the walls of my building (imported from maya with "generate colliders" on).

I've tried adding a rigid body, but the AI falls through the building.

I'm trying to make the AI follow the building mesh as my player would, and would attack upon reaching the player but would not allow the AI to attack continuously as the player can only take 2 hits.

If someone can help me out, I'd greatly appreciate it :D I'm new to coding in c# and unity

here's my code: using UnityEngine; using System.Collections;

 public class EnemyAI : MonoBehaviour {
     
     public float Speed = 0.5f;
     public GameObject ghost;
     
     private float XMove;
     private float YMove;
     private RaycastHit rCast;
     private Transform player;
     private float distanceFromPlayer;
     
     public static EnemyAI Instance;
     private int hp = 100;
     private int attack = 0;
     
     void Awake(){
         Instance = this;    
         Audio.Instance.audio.enabled = false;
     }
     
     void Start(){
         player = GameObject.FindGameObjectWithTag("Player").transform;    
     }
     
     void Update(){
         distanceFromPlayer = Vector3.Distance(transform.position, player.position);
 
         if(distanceFromPlayer <= 0.5f){
             if(attack == 0){
                 attack = 1;
                     ghost.animation.CrossFade("G_Attack");
                 Audio.Instance.audio.enabled = false;
                 //yield WaitForSeconds = 2;
                 //while(ghost.animation.isPlaying){
                     if(Physics.Raycast(transform.position, transform.forward, out rCast)){
                         Debug.Log("Damage Taken");
                         hp = hp - 50;
                         Debug.Log("Player Health = " + hp);
                         
                         if(hp <= 0){
                             Debug.Log("Player DEAD");
                             EnemyAI.Instance.enabled = false;
                         }
                     }
             }
             else{
                 
             }
             //}
             
         }
         else if(distanceFromPlayer < 5){
             //Debug.Log("Chase!");
             transform.rotation = Quaternion.Slerp(transform.rotation, 
                 Quaternion.LookRotation(player.position - transform.position), Time.deltaTime * 4);
             transform.Translate(0,0,.05f);
             ghost.animation.CrossFade("G_Walk");
             Audio.Instance.audio.enabled = false;
             attack = 0;
         }
         else{
             //Debug.Log("Out of Range");    
             ghost.animation.CrossFade("G_Idle");
             ghost.animation["G_Idle"].speed = .5f;
             Audio.Instance.audio.enabled = true;
             attack = 0;
         }
             
             
     
     }
     
 
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

Answer by markedagain · Jul 11, 2013 at 04:14 PM

i hated this problem when i had it.

your problem is that the character controller you are using is setting a global way of checking collisions and only its mesh is used for oncolission checks.

i was making a simple shooting game and the gun would not stop going trough walls, my solution was to stop using the character controller if memory serves me correct

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 fafase · Jul 11, 2013 at 04:15 PM 0
Share

I don't think he is using any character controller and that may be the reason why.

avatar image markedagain · Jul 11, 2013 at 04:19 PM 0
Share

you are correct, actually looking at the code does show using translate and this does not take into account colliders.

avatar image
0

Answer by fafase · Jul 11, 2013 at 04:17 PM

You should look into that: http://unitygems.com/basic-ai-character/

I think your issue is that you are moving the guy with the transform. Even though transform can detect collision, it just does not care about collider and simply gets your object where you tell it to go.

The character controller is a component that has more information for collision and prevent your guy from going through other colliders (as long as you do not travel at war speed).

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 markedagain · Jul 11, 2013 at 04:22 PM 0
Share

Also if u are going to use the physics engine u should do it in the FixedUpdate ins$$anonymous$$d of Update function as its alot of overhead.

and if u still want to stick with doing the movement yourself u will have to do some raycasts to make sure there is nothing in front of you before moving

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

16 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

Related Questions

Enemy not rotating when inside range 1 Answer

How do I make an enemy lead his shots? 2 Answers

2D AI, Aim at player - even when jumping? 1 Answer

Make an enemy only rotate with navmesh agent 1 Answer

2D 360 degress platformer example needed 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