- Home /
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?
Limiting distance so that Object B can't travel more than say 3 units away from Object A in any direction.
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.
Superior knowledge! Thank you very much. Can't upvote yet, as I don't have enough points :(
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.
I agree with you - this is way more useful than any code if you want to operate with physics (colliders).
Your answer
Follow this Question
Related Questions
Tracking distance after Instantiate 2 Answers
Suggestions for AI Selecting From Multiple Targets? 3 Answers
GUI Label script problem??? 1 Answer
making everything fade at distance 2 Answers
How would I decrease a variable over time based on distance? 1 Answer