• 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 shenglee83 · Jan 24, 2013 at 08:37 AM · enemyavoid

Avoid Enemy Ai walk into higher ground terrain or walk on Air

How can i avoid my enemy ai to not walking into the high ground in my terrain, and at the same time i can prevent it not to walk on the air after it move to a higher ground, which i mean it moves based on the terrain's ground height so that it will not walking on the air when it moves to a lower ground..

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 Punkjim420 · Jan 24, 2013 at 09:41 AM

which language? javascript or c#? in javascript id check if the enemy is grounded or not. if it isnt grounded add force to make it "fall" til it hits ground.

in javascript something like this..

 var isGrounded : boolean = false;  //means it is not on the ground
 
 
 function Update(){
     if(!isGrounded){
         rigidbody.AddForce(Vector3.Up * insert a negative number here like -1, -2, -3, etc);
     }
 }
 
 function OnCollisionEnter(col : Collision){
     if(col.gameObject.FindWithTag("Terrain")){
         isGrounded = true;
     }
 }

for this to work youd need a tag on your terrain object called "Terrain" and a rigidbody on the enemy, or you can use transform.Translate instead of rigidbodys. it depends how your monster moves. force, or translations. you will probably have to edit it to fit your scripts a little. If you do it right, as long as the monster isnt standing on your terrain it will push it down if your force is high enough.(should be more than the force used to make the enemy "walk". Hope this helped.

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 Punkjim420 · Jan 24, 2013 at 09:47 AM 0
Share

oh and for the enemy walking into terrain...that shouldnt be possible you might not be using colliders or rigidbodies, could you show your movement script and tell if your objects have colliders/rigidbodies?

avatar image
0

Answer by shenglee83 · Jan 24, 2013 at 09:53 AM

thank you for this script. My enemy object is use character controller actually... i have rigidbody as well..... but anyhow i will try ur script 1st.

Thanks alot.

Comment
Add comment · Show 5 · 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 Punkjim420 · Jan 24, 2013 at 10:05 AM 0
Share

no problem but you shouldnt just use scripts that are given, you should try to understand them so you can write your own. Also, please dont reply using an Answer, theres an "add new comment" button. Use that to reply to other people, unless your posting an answer to somebody elses question.

avatar image shenglee83 · Jan 24, 2013 at 11:49 AM 0
Share

u r right... actually still new in this part. But what i wan to ask again is how the isGrounded variable able to tell the that my enemy ai is on ground or not??? i mean how it will tell update function to addforce to the rigidbody?

avatar image Punkjim420 · Jan 24, 2013 at 12:17 PM 0
Share

isGrounded is a variable that is of type boolean. A boolean can only be true or false. The function OnCollisionEnter(col : Collision) is a function that checks for collision(touch). So this script is setting the boolean(isGrounded) as true if your touching an object tagged as "Terrain" then in the update function we wrote an if statement.

if(!isGrounded){ rigidbody.AddForce(Vector3.Up * insert a negative number here like -1, -2, -3, etc); }

that says if(!isGrounded) or in other words, if isGrounded is false, (your not touching ground) to add force to the rigidbody that your enemy has attached to it.

Does this make any sense to you? Sorry, im not really good at explaining things.

avatar image shenglee83 · Jan 24, 2013 at 01:57 PM 0
Share

that's very helpful.... man, u r good in explaining this... it freshes my mind and i get another understanding from it... Thanks man

avatar image Punkjim420 · Jan 25, 2013 at 12:11 AM 0
Share

no problem, glad i could help.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 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

Related Questions

Object Avoidance for my enemies 1 Answer

How to make enemy avoid others enemies with navmesh? And enemy ai problem. 2 Answers

Ai Alien Zombies get stuck on trees? 0 Answers

Enemy bypass(avoiding)? 0 Answers

top down game,enemies align next to each other 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges