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
0
Question by hfish09 · Mar 01 at 04:31 AM · 2d-platformereffectslines

Creating a trail of characters that follow you

Hello all,

I'm looking to prototype a 2D game that has a mechanic where you have a group of characters that follow you around exactly like this: https://youtu.be/2ejnYn7OZxo?t=182 (Flicky, Sega Genesis)

I'm having trouble wrapping my head around how this effect would be best achieved. My first instinct was that I should create waypoints that follow behind the player and have some AI in each follower that tries to stay as close as possible to it's assigned position. But I have a suspicion that there may be a more simple and efficient way to do this that doesn't involve giving each follower separate AI and physics components.

I don't need code snippets, I'm just looking for a general idea of how I could tackle this effect. Thanks in advance!

Comment
Add comment
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

1 Reply

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

Answer by V_0_1_D_ · Mar 01 at 06:56 AM

An easy way to do this is using Distance Joint 2D. Consider you have a main object and 4 other objects, that must be followed by them continuously. All 5 objects should have a rigid body 2d attached, 4 others (except main object) must have a Distance Joint 2D. Then in connected rigid body field for each of them in the inspector, you must select the object that MUST be followed, for example, select main object for object 1, object 1 for object 2, etc.

The logic behind this is that any object does not exceed maximum distance with it's following object, which can be achieved manually in code with some effort.

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 V_0_1_D_ · Mar 01 at 07:55 AM 0
Share

Here is a sample code.

 using UnityEngine;
     public class FollowTrail : MonoBehaviour
     {
         [SerializeField] Transform following;
         Vector2 currentVelocity;
         float maxDistance = 2.0f;
         float smoothTime = 0.2f;
     
         private void Update()
         {
             float distance = Vector2.Distance(transform.position, following.position);
             if (distance > maxDistance)
             {
                 transform.position = Vector2.SmoothDamp(transform.position, following.position, ref currentVelocity, smoothTime);
     
                 transform.right = Vector2.SmoothDamp(transform.right, following.right, ref currentVelocity, smoothTime);
     
             }
     
         }
     }

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

147 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 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 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 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 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 avatar image avatar image

Related Questions

Hey Guys. I am creating my first 2d Game right now an i run into a problem with sprites. 0 Answers

How to make Mario style Type jump in 2D 0 Answers

I have a 2D platformer runnign with a character, but randomly the Input for the Jump (space) doesn't workds. 0 Answers

2D Jump using "Rigidbody2D.AddForce" doesn't work. 1 Answer

How to prevent a character from being able to jump mid air ? 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