- Home /
How to get an object to follow another object at a set distance in 2D
Hello Wise Ones!
I would like to get object B to follow object A at a set distance. I've attempted to modify the smoothfollow2d.js script like this:
var target : Transform; var smoothTime = 0.3; var xOffset : float = 1.0; var yOffset : float = 1.0;
private var thisTransform : Transform; private var velocity : Vector2;
function Start() { thisTransform = transform; }
function LateUpdate() {
thisTransform.position.x = Mathf.SmoothDamp( thisTransform.position.x, target.position.x, velocity.x, smoothTime) + xOffset;
thisTransform.position.y = Mathf.SmoothDamp( thisTransform.position.y, target.position.y, velocity.y, smoothTime) + yOffset;
}
And it kind of works, but the object follow sort of 'jiggles' and if you look at the inspector, the following objects x, y values are constantly changing (by a very small amount), even though the target object is completely stationary.
I knew it wouldn't be a simple hack! If any of you clever boffins can help me, I'd be much obliged.
Judy x
You can add an if statement to check if you've reached within a certain distance of the object and then snap to that exact location to stop all the small movements. Should be straight forward to implement.
I'm having trouble with this same idea. I understand the code enough to see that it should work, but my 'object b' isn't moving towards my 'object a' at all. Is there something I need to do outside of the code (like add a rigidbody) in order to get my 'object b' to move?
Answer by 3dDude · Jul 28, 2010 at 02:51 PM
you could try this:
var target : Transform; var smoothTime = 0.3; var xOffset : float = 1.0; var yOffset : float = 1.0;
private var thisTransform : Transform;
private var velocity : Vector2;
function Start()
{
thisTransform = transform;
}
function LateUpdate()
{
thisTransform.position.x = Mathf.Lerp( thisTransform.position.x, target.position.x + xOffset, Time.deltaTime * smoothTime);
thisTransform.position.y = Mathf.Lerp( thisTransform.position.y, target.position.y + yOffset, Time.deltaTime * smoothTime);
}
3dDude: You ARE a dude! This is exactly what I needed, many thanks for your time xxxxxxxxx
Except there's one thing. When I do it, my slug thingy is only halfway there. Can anybody help? -Calieb
Answer by Aslan85 · May 21, 2014 at 03:54 AM
Thank you ! It's very helpful!!
Just one question, you declare the private var velocity as a Vector2. What is the utility ?
Vector2 just means there are 2 dimensions. In comparison, Vector3 is used in 3D since there are 3 dimensions.
Answer by timvanderweijde · Jul 02, 2014 at 03:57 PM
Maybe you should also check the distanceJoint2D component instead of scripting away.
I wish I had looked that up before hand, it would have saved me a lot of time. I found out my problem. I was writing the code as if it were 3D (Vector3.forward) rather than 2D (Vector3.right)so there must have been some confusion within the code that made nothing happen at all. It works now.
Thanks for the heads up on the distanceJoint2D though. I also found the springJoint2D which would be cool as well.
Your answer
![](https://koobas.hobune.stream/wayback/20220613065930im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity 2D Object Smooth Follow Problem C# 2 Answers
Why does the camera smoothly follow in one direction but not the other? 0 Answers
Smooth Camera 2D 0 Answers
smooth follow 2d camera runner ios 0 Answers
Problem With 2D Camera Following Target 3 Answers