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 BobbleHead · Jun 12, 2012 at 11:11 AM · raycastaienemy

Enemy to move out of way of player

Hey guys, looking for some help with an idea.

Basicly, i'm hoping to have asome insects on the ground, and when the player moves near then they scutter away, and then when player has walked past they return to the area they were in. All the time, they're movinga bout within a set space.

However, I'm a little stuck. The bugs should be raycasting for object with tag Player - in all directions, however I do not know how this is done. Below is attempt2/34!

Script:

   public var waiting : boolean = false;
     var randomPosition : Vector3;
     var moveDelay : float = 1;
     function Start(){
     }
 
     function Update(){
     var hit : RaycastHit;
         if (Physics.Raycast (transform.position, -Vector3.forward, hit, 1)) {
             var distanceToBall = hit.distance;
             if( distanceToBall <= 1){
              transform.position = Vector3.Lerp (transform.position, randomPosition, Time.deltaTime*10);
         }
     }
 }
 
     function FixedUpdate ()
         {
             if (waiting == false)
             {
                 SendMessage("Move");
             }
                 else
                 {
                 transform.position = Vector3.Lerp (transform.position, randomPosition, Time.deltaTime*1);
                 }
         }
     
     function Move()
         {
             randomPosition = Vector3 (Random.Range(2,2),Random.Range( 100,104 ), Random.Range( -50, -16 ));
             waiting = true;
             yield WaitForSeconds (moveDelay);
             waiting = false;
             //Debug.Log ("waited");
         }
Comment
Add comment · Show 3
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 whydoidoit · Jun 12, 2012 at 11:13 AM 0
Share

Do you need a raycast? Isn't this just a calculation based on the player's position and the bug's position?

avatar image BobbleHead · Jun 12, 2012 at 11:48 AM 0
Share

I'm not sure, I assumed i'd cast a ray, and if the distance between bug and player is less than 1, move out of the way.

avatar image whydoidoit · Jun 12, 2012 at 11:53 AM 0
Share

Well I think @Bazsee has the right idea in the answer he posted after I made the comment. I would have thought just adjusting the position to maintain the $$anonymous$$imum distance using the current vector between the player and the bug would give the effect you are looking for (though you'd have to watch for bugs overlapping each other).

That approach would work well if the bugs look ok moving at the speed of the approaching player or slightly faster.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bazsee · Jun 12, 2012 at 11:23 AM

Maybe you should check for the distance between the player and the bug and if it's smaller then the desired amount, you should do a "negative LookAt", so the bug faces away from the player then move forward some distance. Then you should wait for some seconds, do another check with a bigger distance given, and if the player is not in that radius (ergo moved away), then you should move the bug back.

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 BobbleHead · Jun 15, 2012 at 08:41 AM 0
Share

Now, i got that working, thank you. Too your advice, however do you have any suggestions so that I can cast a ray in all directions, ins$$anonymous$$d of single direction? That;'s my only hurdle now aha

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Stop Enemy "Shooting Through A Wall" 1 Answer

enemy raycast to detect player 1 Answer

Enemy raycast detection 0 Answers

Enemy AI Script Acting Abnormal 0 Answers

AI script works, when we add a blocked function it stops working HELP 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