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 Higgsboson728 · Feb 19, 2017 at 05:46 PM · 3dairigid body

Prevent enemy from falling off edge

Hi,

I have a (3d) game where the enemy charges at the player, but the player can quickly avoid the enemy and just let them fall off the platform they're on. I want the enemies to avoid edges, but I'm not sure how I should start. I'm using rigidbody.AddForce for the enemy movement so I can't just program them to stop since they keep on rolling.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by toddisarockstar · Feb 19, 2017 at 08:45 PM

rigidbody.velocity = Vector3.zero; will stop your enemys if you stop adding force!

have you considered adding walls around your platform and just turning of the renderers so the walls are invisible?

Comment
Add comment · Show 4 · 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 Higgsboson728 · Feb 19, 2017 at 08:50 PM 0
Share

I just want the enemy to avoid edges, so I don't want to completely disable falling.

avatar image toddisarockstar Higgsboson728 · Feb 19, 2017 at 09:30 PM 0
Share

i'm not sure exactly what your levels / platforms look like. you could assign marker points in your platform/level so that your AI script had a way of knowing it was close to your edges. i have code for picking up location markers if you that would help you.

avatar image Higgsboson728 toddisarockstar · Feb 19, 2017 at 09:40 PM 0
Share

$$anonymous$$y platform is a square shape, so nothing complex... I also want my script to be compatible with other platforms, so that I can easily reuse them without needing to create scripts for each platform.

avatar image toddisarockstar · Feb 22, 2017 at 02:25 AM 0
Share

This Is a good universal script to know if items are within a designated box shaped area. i wrote it for 3d so it detects objects within x and z positions. it ignores up and down though that could be easily added. to use this code you simply make at least two empty game objects in your scene and name them "marker0" and "marker1". then move the markers to opposite corners of the box shaped area you would like to define. you can assign as many areas as you like as long as they are named in order. you only use two markers per area so if you want to add a second area, you would create "marker2" and "marker3" and so on. if your object is not within any area the script returns 100.

hopefully this is helpfull to know if you want to detect where your enemy objects are so you can make them turn around our whatever you want them to do.

it doesn't use any functions or colliders or physics its just simple math so it will work with any device/machine.

     int i;
     public Vector3[] markers;
     bool b;
     int rc;
     Vector3 v;
 
     public int marker;
 
 
     void Start () {
 
         b = false;
         i = 0;
         while (!b) {if (GameObject.Find ("marker" + i)) {i++;} else {b = true;}} 
         markers = new Vector3[i];
         while (i>0) {i--;markers[i] = GameObject.Find ("marker"+i).transform.position;}
         b = false;
         i = 0;
     }
     
     // Update is called once per frame
     void Update () {
 
 
         v = transform.position;
 
         marker = 100;
         rc = 0;
         while(rc!=200){i = 0;
             if(markers[rc].x<markers[rc+1].x){
                 if(v.x>markers[rc].x){if(v.x<markers[rc+1].x){i++;}}}
             else{if(v.x>markers[rc+1].x){if(v.x<markers[rc].x){i++;}}}
             if(i==1){  
                 if(markers[rc].z<markers[rc+1].z){
                     if(v.z>markers[rc].z){if(v.z<markers[rc+1].z){i++;}}}
                 else{if(v.z>markers[rc+1].z){if(v.z<markers[rc].z){i++;}}}
             }
             if(i==2){print(rc);marker=rc;rc=200;}
             rc=rc+2;
             if(rc>markers.Length-2){rc=200;}
         }
     
     }
 
avatar image
0

Answer by Zee-Play · Feb 22, 2017 at 02:52 AM

From what I understood you want the enemies to stop before hitting the edge of your platform. Can just do a rayscan each frame like this and check if it collides with the platform, having the vertical rayscan always be done in front of your character (the direction he's moving)

If the rayscan returns null, then you stop all forces on the object and rigidbody.velocity = Vector3.zero like toddisarockstar said.

alt text


ray.jpg (76.2 kB)
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

132 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Simple AI for Unity to patroll and follow the player 0 Answers

Help to make enemy AI not go through terrain and not fly when chasing player up hills. 0 Answers

How to keep vehicles in correct lanes 0 Answers

Where to start learning about AI? 1 Answer

Problems with basic enemyAI on sphereical world 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