Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
5
Question by fulcanmal · Jan 19, 2011 at 06:59 PM · distance

Limiting Distance

Object A travels along a set path. It's motion will be added to Object B so that Object B will follow without exactly mirroring the position of Object A. Object B will be given controls to move, but I want to prevent it from traveling a certain distance from Object A.

How can I limit the distance between these Objects?

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 DaveA · Jan 19, 2011 at 07:51 PM 0
Share

Within a cirlce, sphere, box, straight line??

avatar image fulcanmal · Jan 19, 2011 at 09:44 PM 0
Share

Limiting distance so that Object B can't travel more than say 3 units away from Object A in any direction.

avatar image fireDude67 · Jan 19, 2011 at 11:22 PM 0
Share

$$anonymous$$ath.Clamp()?

2 Replies

· Add your reply
  • Sort: 
avatar image
10

Answer by Flynn · Jan 19, 2011 at 10:34 PM

Good question. Your choice: Trigonometry or Transformation.

Trigonometry: What you need to do is detect the distance between your center object and the object that cannot be 3 units away.

If it is further away than it should be, calculate its positions angle in relative toe the center object, then use trigonometry to calculate a position at that angle exactly 3 units away!

Transformation: Use translation to move it back. How?

Subtract the position of the restricted object from the center object. Then get that vector's magnitude.

After that, multiply that magnitude by the distance between the two objects minus 3.

Finally, add the returned vector to transform.position :)

Code for transformation:

JS:

//Restricts the position of this object to be within a distance of "distance" of "center"
public var distance : float = 3;
public var center : Transform;
function Update ()
{
    var dst = Vector3.Distance(center.position, transform.position);
    if (dst > distance)
    {
        var vect : Vector3 = center.position  - transform.position;
        vect = vect.normalized;
        vect *= (dst-distance);
        transform.position += vect;
    }
}

C#:

using UnityEngine; using System.Collections;

public class RestrictMe : MonoBehaviour {

 //Restricts the position of this object to be within a distance of "distance" of "center"
 public float distance = 3;
 public Transform center;
 private void Update ()
 {
     float dst = Vector3.Distance(center.position, transform.position);
     if (dst > distance)
     {
         Vector3 vect = center.position  - transform.position;
         vect = vect.normalized;
         vect *= (dst-distance);
         transform.position += vect;
     }
 }

}

Sorry I can't provide an example of the trigonometry version. Too complex in my opinion, and also really only works well in 2D.

Comment
Add comment · Show 5 · 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 bradmarxmoosepi · Jun 15, 2012 at 06:03 PM 0
Share

Thanks a lot: really helpful!

avatar image Sanky · Feb 07, 2013 at 01:26 PM 0
Share

great man superb WOR$$anonymous$$..!

avatar image Ultroman · Jan 18, 2015 at 04:15 PM 0
Share

Superior knowledge! Thank you very much. Can't upvote yet, as I don't have enough points :(

avatar image mcarrascosh · Feb 08, 2018 at 01:09 AM 0
Share

Great method! thanks a lot!

avatar image mcarrascosh · Feb 08, 2018 at 01:09 AM 0
Share

Great method! thanks a lot!

avatar image
3

Answer by pixelmixer · Jan 19, 2011 at 10:35 PM

If you are using Physics you can attach a SpringJoint to the objects and set Max Distance for the Spring Joint to 3 units. That will basically restrict an object from going beyond a 3 unit sphere. If you don't use gravity and set the Spring property to 0 it will just float around until it reaches it's limit. You can play around with the settings a bit to get it to work with your project.

This will invariably be faster than most any script you can write in JS or C# and will likely save you some time writing the math for it.

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 Kerel · Apr 07, 2014 at 04:03 PM 1
Share

I agree with you - this is way more useful than any code if you want to operate with physics (colliders).

avatar image mwfelker · Feb 03 at 03:29 AM 0
Share

Thanks this work perfectly!

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

How to calculate direction between 2 objects 2 Answers

Endless runner distance 2 Answers

Distance from raycast. 1 Answer

Frustum culling question 1 Answer

White textures when objects are really far away on android? 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