• 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 samaxi · Feb 02, 2014 at 06:19 PM · javascriptmovementrigidbodyai

AI walk through solid walls

The AI in my FPS walk right through solid walls, I've box colliders, mesh colliders, rigid bodies but I can't get them to not walk through solid walls. Please help me out.

T$$anonymous$$s is my AI script.

 var distance;
 var target : Transform;    
 var lookAtDistance = 15.0;
 var stopDistance = 3;
 var moveSpeed = 5.0;
 var damping = 6.0;
  
 var damage:float = 17;
 var range = 100.0;
 var force = 10.0;
 var muzzleFlash : Renderer;
 var muzzleLight : Light;
  
  
 var Health = 100;
 var deadReplacement : Transform;
 var dieSound : AudioClip;
  
 static var isDead = false;
  
 static var allowfire : boolean = true;
  
 function Start (){
 muzzleFlash.enabled = false;
 muzzleLight.enabled = false;
 allowfire = true;
 }
  
 function Update () {
     target = GameObject.FindWithTag("Player").transform;
     distance = Vector3.Distance(target.position, transform.position);
  
     if((distance < lookAtDistance)){
     lookAt ();
     }
     if(allowfire == true){
     fire();
 }
 }
  
  
 function lookAt (){
     var rotation = Quaternion.LookRotation(target.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
     transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
     animation.Play("walk");
 }
  
 function ApplyDammage (TheDammage : int)
 {
     Health -= TheDammage;
  
     if(Health <= 0)
     {
        Dead();
     }
 }
  
 function Dead () {
     // Destroy ourselves
     Destroy(gameObject);
     Destroy(transform.parent.gameObject);
  
     // Play a dying audio clip
     if (dieSound)
     AudioSource.PlayClipAtPoint(dieSound, transform.position);
 }
  
 static function CopyTransformsRecurse (src : Transform,  dst : Transform) {
     dst.position = src.position;
     dst.rotation = src.rotation;
  
     for (var c$$anonymous$$ld : Transform in dst) {
        // Match the transform with the same name
        var curSrc = src.Find(c$$anonymous$$ld.name);
        if (curSrc)
          CopyTransformsRecurse(curSrc, c$$anonymous$$ld);
     }
 }
  
 function fire(){
 allowfire = false;
 audio.Play();
  
 var direction = transform.TransformDirection(Vector3.forward);
 var $$anonymous$$t : RaycastHit;
  
 // Did we $$anonymous$$t anyt$$anonymous$$ng?
 if (Physics.Raycast (transform.position, direction, $$anonymous$$t, range)) {
 Debug.DrawRay(transform.position, direction * range, Color.green);
 // Apply a force to the rigidbody we $$anonymous$$t
 if ($$anonymous$$t.rigidbody)
 $$anonymous$$t.rigidbody.AddForceAtPosition(force * direction, $$anonymous$$t.point);
 $$anonymous$$t.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
 }
  
 muzzleFlash.renderer.enabled = true;
 muzzleLight.enabled = true;
 yield WaitForSeconds (0.04);
 muzzleFlash.renderer.enabled = false;
 muzzleLight.enabled = false;
  
  
 yield WaitForSeconds(0.3);
 allowfire = true;
 }
  
 function OnTriggerEnter($$anonymous$$t:Collider){
 if($$anonymous$$t.tag == "Player"){
 $$anonymous$$t.transform.SendMessage("Damage",damage);
 }
 }
Comment
Add comment · Show 1
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 jg2115 · Feb 02, 2014 at 06:55 PM 0
Share

Maybe make the rigidbody's collision mode "continuous dynamic"

1 Reply

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

Answer by fafase · Feb 02, 2014 at 07:23 PM

You are moving with Translate w$$anonymous$$ch does not consider collision unless you make the code for it. You should go for the Character Controller.

http://unitygems.com/basic-ai-character/ t$$anonymous$$s has a tutorial with what is about t become the old Character Controller since Unity is developing a new version. But that should get you going.

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 samaxi · Feb 02, 2014 at 08:33 PM 0
Share

How would I change this to CC.move/simple move?

avatar image fafase · Feb 03, 2014 at 07:25 AM 0
Share

By reading the tutorial link.

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

18 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Unity3D Ai wont shoot 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Enemy AI With changing Player 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