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
1
Question by Nercoe · Oct 23, 2012 at 11:22 AM · movementrandomlimit

Random movement, staying in an area.

Hey I have acquired a script that allows for random movement, the problem is my object just wanders off the plane and ignores collision. Is there anyway I can set a boundary for this random movement to stop it venturing off the plane and into dead space? Thanks, here's what I have so far:

 var chicken : Transform; 
 var vel : Vector3;
 var Direction : float = 3;
 var curTime : float = 0;
 function Start()
 {
   SetVel();
 }
 function SetVel()
 {
   if (Random.value > .5) {
     vel.x = 10 * 10 * Random.value;
   }
   else {
     vel.x = -10 * 10 * Random.value;
   }
   if (Random.value > .5) {
     vel.z = 10 * 10 * Random.value;
   }
   else {
     vel.z = -10 * 10 * Random.value;
   }
 }
 
 function Update()
     {
           if (curTime < Direction) {
           curTime += 1 * Time.deltaTime;
   }
   
   else {
         SetVel();
             if (Random.value > .5) {
               Direction += Random.value;
 }
 
   else {
       Direction -= Random.value;
   }
         if (Direction < 1) {
           Direction = 1 + Random.value;
     }
     curTime = 0;
   }
 }
 
 
 function FixedUpdate()
     {
   chicken.rigidbody.velocity = vel;
 }



Any help is appreciated :)

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
4

Answer by Rosonator · Oct 23, 2012 at 11:41 AM

This is what I did 2 hours ago. It allows a random movement throught a surface which limits I have established manually. It sets the correct rotation of the object too, but be careful with the correction of 90º degrees I had to do and apply yours. I hope this helps to fix your code.

 public float velocidadMax;
     
     public float xMax;
     public float zMax;
     public float xMin;
     public float zMin;
         
     private float x;
     private float z;
     private float tiempo;
     private float angulo;
 
     // Use this for initialization
     void Start () {
 
 
         x = Random.Range(-velocidadMax, velocidadMax);
         z = Random.Range(-velocidadMax, velocidadMax);
         angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
         transform.localRotation = Quaternion.Euler( 0, angulo, 0);
     }
     
     // Update is called once per frame
     void Update () {
 
         tiempo += Time.deltaTime;
 
         if (transform.localPosition.x > xMax) {
             x = Random.Range(-velocidadMax, 0.0f);
             angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
             transform.localRotation = Quaternion.Euler(0, angulo, 0);
             tiempo = 0.0f; 
         }
         if (transform.localPosition.x < xMin) {
             x = Random.Range(0.0f, velocidadMax);
             angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
             transform.localRotation = Quaternion.Euler(0, angulo, 0); 
             tiempo = 0.0f; 
         }
         if (transform.localPosition.z > zMax) {
             z = Random.Range(-velocidadMax, 0.0f);
             angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
             transform.localRotation = Quaternion.Euler(0, angulo, 0); 
             tiempo = 0.0f; 
         }
         if (transform.localPosition.z < zMin) {
             z = Random.Range(0.0f, velocidadMax);
             angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
             transform.localRotation = Quaternion.Euler(0, angulo, 0);
             tiempo = 0.0f; 
         }
 
 
         if (tiempo > 1.0f) {
             x = Random.Range(-velocidadMax, velocidadMax);
             z = Random.Range(-velocidadMax, velocidadMax);
             angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
             transform.localRotation = Quaternion.Euler(0, angulo, 0);
             tiempo = 0.0f;
         }
 
         transform.localPosition = new Vector3(transform.localPosition.x + x, transform.localPosition.y, transform.localPosition.z + z);
     }
Comment
Add comment · Show 8 · 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 Nercoe · Oct 23, 2012 at 12:43 PM 0
Share

I use JavaScript

avatar image Rosonator · Oct 23, 2012 at 02:47 PM 0
Share

Well, you can use what you want, but this script works and you won't have any problem combining a C# script with other JS scripts, because that's what UNity does great: abstract from the code. And if by any case you still need to add this feature to a JS script sure that you can understand that C# code and translate what you need to JS by your own, right?

avatar image umitems · Mar 17, 2014 at 04:38 AM 0
Share

I've tried your c# code the object can move only one time then no move. I also log the variable tiempo this variable is always zero every update. I also changed from tiempo += Time.deltaTime; to tiempo += 2; but nothing gonna happen tiempo still zero. What shall I do?

avatar image bariscigal · Mar 25, 2014 at 07:51 PM 0
Share

@Nercoe Sorry but you are asking too much. You cant find lots of people answering with an exact code. At least a thanks would keep this community on its feet.

@Rosonator Thanks a lot. This was what i was looking for.

avatar image coolvin · Aug 28, 2014 at 02:45 PM 0
Share

@Rosonator Thanks for Sharing.. how to control the speed..??

avatar image gaurangranoliya coolvin · Oct 05, 2015 at 05:29 AM 0
Share

@coolvin In this script decrease your public float velocidad$$anonymous$$ax variable..

Show more comments
avatar image
0

Answer by sundhar_unity · Oct 27, 2020 at 04:49 AM

Check this Video For Random MoveMent : https://youtu.be/BIyYldTyyR8

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

17 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

Related Questions

Issue with clamping player to camera view 2 Answers

random direction of enemy when collide whit wall 1 Answer

How to stop MoveTowards teleporting 1 Answer

set limit mouse x y Help needed 1 Answer

Make an object move towards random spot on another objects edge? 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