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 Theacesofspades · Mar 27, 2013 at 06:52 PM · javascriptraycast3denemy ai

Help with Enemy AI

So i made a basic "feeler" enemy AI that is only 2d which means it doesnt go up or down on the y axis. But now i would like to make it be able to walk up stairs or a ramp by felling around. Can anyone point me in the right direction on this? The 2d script im using now is:

 var moveSpeed = 3;
 var rotateSpeed = 10;
 var checkDistance = 3;
 
 var rightCheck : Transform;
 var enemyChecker : Transform;
 var leftCheck : Transform;
 
 @HideInInspector
 var target : Transform;
 @HideInInspector
 var controller : CharacterController;
 
 
 
 
 function Start ()
 {
     target = GameObject.FindWithTag("Player").transform;
     
     controller = GetComponent(CharacterController);
 }
 
 function Update () 
 {    
     enemyChecker.transform.LookAt(target);
     enemyChecker.transform.rotation.x = 0;
     enemyChecker.transform.rotation.z = 0;
     
     
     
     var hit : RaycastHit;
     
     
     if (!Physics.Raycast(enemyChecker.transform.position, enemyChecker.transform.forward, hit, checkDistance))
     {
         transform.rotation = Quaternion.RotateTowards(transform.rotation, enemyChecker.transform.rotation, rotateSpeed/2);
     }
     
     if (Physics.Raycast(leftCheck.transform.position, leftCheck.transform.forward, hit, checkDistance))
     {
         transform.Rotate(Vector3.up * rotateSpeed);
     }
     
     if (Physics.Raycast(rightCheck.transform.position, rightCheck.transform.forward, hit, checkDistance))
     {
         transform.Rotate(Vector3.up * -rotateSpeed);
     }
     
     if (Physics.Raycast(leftCheck.transform.position, leftCheck.transform.forward, hit, checkDistance) && Physics.Raycast(rightCheck.transform.position, rightCheck.transform.forward, hit, checkDistance))
     {
         transform.Rotate(Vector3.up * rotateSpeed);
     }
     
     
     controller.SimpleMove(transform.forward * moveSpeed);
     
 }
 
 @script RequireComponent(CharacterController)

Can anyone point me in the right direction? Thanks!

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

Answer by fafase · Mar 27, 2013 at 07:00 PM

Two ways

  1. You make the stairs small enough so that the CC can "climb up" -> not so good though is it?

  2. You remove the colliders from the stairs and create a slope collider of one box collider inclined -> good point, it looks the same and your player walks fine on it. You can have a basic check to mak him play a walkingUpTheStairs animation despite the fact he is walking a flat slope. Bad point, you have to remove all colliders from your steps...

Comment
Add comment · Show 3 · 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 Theacesofspades · Mar 27, 2013 at 07:10 PM 0
Share

Thanks, but how could i tell the AI that he needs to go up the stairs? Because right now the enemy will go to underneath the player, The player is on the 2nd story and the AI will be right underneath him on the first story of the building.

avatar image fafase · Mar 27, 2013 at 07:28 PM 0
Share

Do you mean the AI is able to pass the stairs or walk the stairs depending on its state? If so, As you are in 2D, you could have the AI at z = 0 but the stairs at z=1 when the state is on chasing meaning you want him to go up, then you have a trigger box just at the bottom of the stairs that checks the state of the AI and "pushes" him of 1 in the z axis if he is chasing. Now your Controller will do the rest. At the top same process backward.

avatar image Theacesofspades · Mar 28, 2013 at 01:29 AM 0
Share

No it can walk up but the problem is that it doesn't know to walk up the stairs it just goes to underneath the player.

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Accessing colliders, in if statement 2 Answers

help unity3d javascript 1 Answer

Make Raycast that ignores certain tags? 0 Answers

Raycasting2D please help 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