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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by ardizzle · Nov 14, 2014 at 05:06 PM · c#transformmove

Keep a min distance away from object while moving towards it

I have game where a player moves in spaces kinda like a chess board. And every time the player moves two objects also move following him. How I did this is I have him move with the press of an arrow key and then I put a script on the other objects that say when an arrow key is pressed move towards the object and it works great. But I need to keep them from getting to close to him and this is where im having trouble. I tried to do a set up like this:

             if(transform.position.x >= 0)
             {
                 if(Mathf.Abs(currCam.transform.position.x) - Mathf.Abs(transform.position.x) < minDistance)
                 {
                     float setFromCamX = (Mathf.Abs(currCam.transform.position.x) - Mathf.Abs(transform.position.x) - minDistance) * 1;
                     transform.position = new Vector3(transform.position.x + setFromCamX, transform.position.y, transform.position.z);
                 }
             }
             else
             {
                 if(Mathf.Abs(currCam.transform.position.x) - Mathf.Abs(transform.position.x) < minDistance)
                 {
                     float setFromCamX = (Mathf.Abs(currCam.transform.position.x) - Mathf.Abs(transform.position.x) - minDistance) * -1;
                     transform.position = new Vector3(transform.position.x + setFromCamX, transform.position.y, transform.position.z);
                 }
             }

What that does is it checks where both objects are at, then it will subtract or add distance to set them a mid distance away from him and keep the object on the correct side. But my objects always end up as a diagonal away from him. So what is a better way to approach this problem?

Comment
Add comment · Show 6
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 richyrich · Nov 14, 2014 at 06:04 PM 0
Share

Your question is about 'flocking'. I've not needed to use it myself, but hopefully this will help you:

http://forum.unity3d.com/threads/maintain-$$anonymous$$imum-distance-from-others.49604/

avatar image Inan-Evin · Nov 14, 2014 at 06:13 PM 0
Share

may i ask you how did you maintain the movement of your player ? actually using the same method to move all objects and using same speed variables should end up with linear and no distance reducing movement.

avatar image ardizzle · Nov 14, 2014 at 08:04 PM 0
Share

Player movement is based on already set coordinates in the game. Object movements are done through Vector3.$$anonymous$$oveTowards

avatar image ardizzle · Nov 14, 2014 at 08:07 PM 0
Share

And that link was close but not quite what i need. The problem is the enemys bunch up on the player

avatar image blastintyres · Nov 15, 2014 at 02:39 PM 0
Share

This should help.....I THIN$$anonymous$$ https://www.assetstore.unity3d.com/en/#!/content/21626 https://www.assetstore.unity3d.com/en/#!/content/14437

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

29 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

Related Questions

Move Object A's Y position towards Object B's Y position 1 Answer

C# move y position of object not working 2 Answers

C#: Move a gameobject from a random location? 1 Answer

Distribute terrain in zones 3 Answers

How can i set a radius for rotatearound ? 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