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 gilgada · Feb 28, 2013 at 07:04 PM · airandombehaviourfinite-state-machine

Getting my object to rotate and move at random

In my scene I have a predator and a prey. At the moment the predator will chase the prey when the prey is within it's range of sight. When the prey is not in sight, I want the predator to exhibit some random behaviour, walking around randomly. It's growing very problematic and inefficient (while loops linked to FixedUpdate, causing some crashes).

What the code is supposed to do is rotate the predator on the spot for a randomly defined amount of time and then once that has done, see if it can move to a randomly generated target position. This target position is the position of an empty gameobject (only has a box collider for linecast). The position is changed using the SetTargetPosition method. If the target position has a collider in the way, it should be generated again.

The code is as follows, starting with the 'else' statement that occurs when the prey isn't in sight: else{ //predator must not have prey in sight, do random movements

             if(isTurning == false)    //check if the predator is already turning around, this is false to start with
             {
                 if(Physics.Linecast(transform.position, target.transform.position, out hit2)
                    //performs a linecast between predator and target position
                 && hit2.transform == target.transform)
                 {
                     sightDirection = targetPosition - transform.position;
                     //set the line between the predator and the target
                     velocity = sightDirection.normalized * movementSpeed;
                     rndmTurn = Random.Range(0, 2);    //range to determine which way to turn the predator
                                                 //maximum is exclusive value, will return either 1 or 0
                     turnMultiplier = 100;//Random.Range(10, 100);
                     //random number for how much to turn the predator by
             
                     while(timer <= turnMultiplier)    //while
                     {    
                         isTurning = true;
                 
                         if(rndmTurn == 0)    //use as left
                         {        
                             Debug.Log ("random = " + rndmTurn + ", range = ");
                             predatorRotation = this.transform.localEulerAngles;
                             predatorRotation.y += rotSpeed * Time.deltaTime;
                             this.transform.localEulerAngles = predatorRotation;
                         }
                         else {                //use as right
                             Debug.Log ("random = " + rndmTurn + ", range = ");
                             predatorRotation = this.transform.localEulerAngles;
                             predatorRotation.y -= rotSpeed * Time.deltaTime;
                             this.transform.localEulerAngles = predatorRotation;
                         }
                     }
                     timer ++;
                 
                     if(this.transform.position != targetPosition)
                     {
                         transform.Translate(velocity * Time.deltaTime, Space.World);
                     }
                     else
                     {
                         timer = 0;
                         isTurning = false;
                     }
                 }
                 else
                     {
                 target.transform.position = SetTargetPosition();
                     }

             }
         }

Vector3 SetTargetPosition() { targetPosition.x = Random.Range (-25,60); //x targetPosition.y = this.transform.position.y; //y targetPosition.z = Random.Range (-45,25); //z

     return targetPosition;
 }


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 llSalvationll · Feb 28, 2013 at 08:49 PM

Firstly, have you determined what specifically is causing the crashes (is it the while loop not completing, or something else)? And secondly, is there anything else you are asking help with other than the performance (i.e. is there anything else here that doesn't work as you would like it to)?

On the surface though, I can see where if your random positions that you generate continue to be obstructed and you have a lot of objects that can obstruct you, that could have some pretty poor performance implications. What you might consider doing is being a little bit less 'random' when moving your target's position. It may help to have a defined set of directions from the predator's current location that you can check for (i.e. forward/back/left/right and in between) instead of randomly generating points to try within a given 'box' each time - you can also check to make sure that the points you are testing are within the x and z ranges you want without much overhead (if that is a concern).

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 gilgada · Feb 28, 2013 at 10:33 PM 0
Share

I'd really just like a better way to get it to move and rotate at random, as one state, so when not chasing. The predator has a property for how far it can 'see' which can be used to add a limit to how far to move it maybe.

avatar image gilgada · Feb 28, 2013 at 11:50 PM 0
Share

I'll figure something out, working on just using booleans ins$$anonymous$$d of the loops. I'll probably end up using the current position of the predator to add to the random.range of the new target.

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

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

Move Enemy randomly in a range. 3D 0 Answers

NavMesh.SamplePosition returns point outside the NavMesh 0 Answers

How to make NPCs check the time? 1 Answer

Can you randomise a Navmesh agents rotation 1 Answer

Racing Ai? long read 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