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 Vipul_Gandhi · Mar 02, 2015 at 12:03 PM · collisionmovementgamedistance

Keep minimum distance while moving.

Hi Guys, Hope you are doing well. I have struck in a problem, I have googled it but did not found any satisfactory answer. My objective is to move different gameobjects randomly, like cats are moving in a room but want to keep a minimum distance among them.

I have tried the following code but the gameobject jerks while playing.

 #pragma strict
 var Kitten_RandomPosition : Vector3;
 var HouseCat_RandomPosition : Vector3;
 
 public var Kitten : GameObject;
 public var HouseCat : GameObject;
 
 InvokeRepeating("ChangeKittenPosition", 0, 1);
 InvokeRepeating("ChangeHouseCatPosition", 0, 1.4);
 
 function Start () 
 {
     Kitten_RandomPosition = Vector3(Random.Range(-8.0, 8.0),Random.Range(-4.0, 4.0), 0);
     HouseCat_RandomPosition = Vector3(Random.Range(-8.0, 8.0),Random.Range(-4.0, 4.0), 0);
 }
 
 function Update () 
 {
     
     if((Vector3.Distance(Kitten.transform.position, HouseCat.transform.position))<=2)
     {
         ChangeKittenPosition();
     }
     
     
     
     Kitten.transform.position = Vector3.MoveTowards(Kitten.transform.position, Kitten_RandomPosition, 2*Time.deltaTime) ;
     PlumpCat.transform.position = Vector3.MoveTowards(PlumpCat.transform.position, PlumpCat_RandomPosition, 2*Time.deltaTime) ;
 
 
 }
 
 
 function ChangeKittenPosition()
 {
     Kitten_RandomPosition = Vector3(Random.Range(-8.0, 8.0),Random.Range(-4.0, 4.0), 0);
 }
 
 function ChangeHouseCatPosition()
 {
     HouseCat_RandomPosition = Vector3(Random.Range(-8.0, 8.0),Random.Range(-4.0, 4.0), 0);
 }

Any Idea would be appreciated. Thank you

Comment
Add comment · Show 8
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 gjf · Mar 02, 2015 at 11:08 AM 0
Share

please format ALL of your code.

avatar image gjf · Mar 02, 2015 at 11:41 AM 0
Share

not quite. highlight ALL of your code in the original question and click on the 101/010 button at the top of the edit box. without proper formatting your code is hard to read and unlikely to get anyone to help.

please delete your comment once you've fixed the original question.

avatar image Vipul_Gandhi · Mar 02, 2015 at 11:53 AM 0
Share

Hope it helps !!

avatar image sniper43 · Mar 02, 2015 at 12:36 PM 0
Share

You should use an array of cats to avoid having to assign a variable for each cat.

 public Cat[] Cats;

I don't know what the JS equivalent is, since I don't personally like the syntax so I never bothered. You can use a dictionary if you want to have named cats. Again, don't know what the JS equivalent is.

avatar image Vipul_Gandhi · Mar 02, 2015 at 12:46 PM 0
Share

I have done this, thanks for the advice, but the issue remains i.e. "How to keep a $$anonymous$$. distance b/w them while moving".

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unreliability of Physics.Raycast.Distance for player movement? 0 Answers

Rigidbody slows down before touching its target 1 Answer

How to get transform.Translate to work with rigidbodies 2 Answers

Enemy Move towards object and hold it and use object 1 Answer

Player glitching through wall when sprinting 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