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
3
Question by JudyNails · Jul 28, 2010 at 12:34 PM · 2ddistancefollowsmoothset

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

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 spinaljack · Jul 28, 2010 at 03:00 PM 0
Share

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.

avatar image JudyNails · Jul 28, 2010 at 09:57 PM 0
Share

Thanks for taking a look spinaljack - appreciate it :)

avatar image Piko-Island-Studios · Jul 02, 2014 at 03:22 PM 0
Share

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?

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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);

 }

Comment
Add comment · Show 4 · 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 JudyNails · Jul 28, 2010 at 09:57 PM 0
Share

3dDude: You ARE a dude! This is exactly what I needed, many thanks for your time xxxxxxxxx

avatar image joel_b · May 23, 2013 at 10:02 PM 0
Share

Extremely helpful!

avatar image Sang Cha · Jun 13, 2014 at 03:17 PM 0
Share

Thank you. Works great!

avatar image MasterChief333 · Nov 22, 2014 at 09:12 PM 0
Share

Except there's one thing. When I do it, my slug thingy is only halfway there. Can anybody help? -Calieb

avatar image
0

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 ?

Comment
Add comment · Show 1 · 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 General-Troll · Jun 26, 2014 at 09:33 PM 0
Share

Vector2 just means there are 2 dimensions. In comparison, Vector3 is used in 3D since there are 3 dimensions.

avatar image
0

Answer by timvanderweijde · Jul 02, 2014 at 03:57 PM

Maybe you should also check the distanceJoint2D component instead of scripting away.

Comment
Add comment · Show 1 · 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 Piko-Island-Studios · Jul 02, 2014 at 04:55 PM 1
Share

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

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

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


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