• 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
Question by RaulG · Jan 14, 2015 at 07:04 PM · movementtransformai

Multiple objects follow the object in front

 private float amplitudeY = 3.0f;
     private float omegaZ = 5.0f;
     private float omegaY = 1.0f;
     private float timeI;
     public EnemySpawn eSpawn;
 
     public int place;
 
     //keeps current position of objects
     public List<Vector3> posList = new List<Vector3>();
 
     public void Snaker()
     {
         Vector3 leader = eSpawn.eSnake [0].transform.position;
 
         posList.Add (new Vector3 (eSpawn.eSnake[0].transform.position.x,
                                   eSpawn.eSnake[0].transform.position.y,
                                   eSpawn.eSnake[0].transform.position.z));
         Vector3 v = posList [0];
         Vector3 toLeader = leader - transform.position;
         Quaternion wantDir = Quaternion.LookRotation(toLeader);
 
         timeI += Time.deltaTime;
         float z = omegaZ * timeI;
         float y = Mathf.Abs (amplitudeY * Mathf.Sin (omegaY * timeI));
         eSpawn.eSnake [place].transform.position = new Vector3 (0.0f, v.y - y, v.z - z);
         transform.rotation = Quaternion.RotateTowards(transform.rotation, wantDir, 360*Time.deltaTime);
         transform.position = leader - (toLeader.normalized * 1.5f);
 
 
     }

Currently thats what I have.

Unfortunately what happens is object 1 follows object 0 just fine, but objects 2-5 all stack onto object 1.

How can I add a space between each of the objects?

Comment

People who like this

0 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 Noah Dyer · Jan 14, 2015 at 07:31 PM 0
Share

Where is place set?

avatar image Noah Dyer · Jan 14, 2015 at 07:32 PM 0
Share

Also, what function is this happening in?

avatar image RaulG · Jan 14, 2015 at 07:33 PM 0
Share

Its in it's own function, and place is a public int.

4 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Noah Dyer · Jan 14, 2015 at 07:38 PM

I believe you problem is your toLeader. For all your other variables, you appear to be determining relations$$anonymous$$ps through your through an array, but your toLeader variable is set to the specific object that t$$anonymous$$s script is attached to. my best guess is that the line needs to become

 Vector3 toLeader = leader - eSpawn.eSnake [place].transform.position;

Comment

People who like this

0 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 RaulG · Jan 14, 2015 at 07:42 PM 0
Share

Added that, the object just sits there.

If I add

 place++;
 if(place >=5)place = 0;


the objects just snap all over the place.

avatar image Noah Dyer · Jan 14, 2015 at 08:32 PM 0
Share

When is Snaker() called? Do all 5 objects (the leader and followers) have this script? You're setting place in the editor then?

avatar image RaulG · Jan 14, 2015 at 09:10 PM 0
Share

public void Snaker()

Yes

No. I tried both with and without Place++ to check if either would work.

I'm thinking about using Dicitionaries? Would that work?

avatar image Noah Dyer · Jan 15, 2015 at 12:42 AM 0
Share

@NasarethMekuri's logic helped me figure out what I think your problem actually is. You're always following the in index 0. Instead, you should follow "place - 1", no?

avatar image RaulG · Jan 15, 2015 at 12:47 AM 0
Share

Wouldn't that just do the same, except for the last object in the list?

Or give me an Argument out of range error.

avatar image

Answer by khos85 · Jan 14, 2015 at 10:38 PM

Why not just use built in unity $$anonymous$$nges on each object with a spring on each?

Comment

People who like this

0 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 RaulG · Jan 14, 2015 at 10:44 PM 0
Share

Elaborate on what that is.

avatar image RaulG · Jan 14, 2015 at 10:53 PM 0
Share

Oh it's used with Rigidbodies. Those don't agree to much when it comes to being easy to work with.

avatar image khos85 · Jan 14, 2015 at 11:48 PM 0
Share

why not ? :) what issue have you got with hinges, can you provide more details? I reckon that would work nicely if you got the hinges configured well..

avatar image RaulG · Jan 15, 2015 at 12:03 AM 0
Share

Yeah Im trying to get them to work, but I don't really understand what I'm suppose to do with them.

Like how am I suppose to get the objects to move forward, all they do is rotate.

Or why won't the Connected Body connect to the rigidbody?

avatar image RaulG · Jan 15, 2015 at 12:09 AM 0
Share

If possible, can I parent the "Head" object to a node, and attack a rigidbody to the node, then connect the "Body" objects to the node?

avatar image

Answer by MKayJay · Jan 14, 2015 at 10:58 PM

I believe t$$anonymous$$s is some kind of snake game? Wouldn't it be easier for you to keep track of how "long" your snake is, and then use that number to make the object follow the one just in front of it? So object 1 follows object 0- object 2 follows object 1, and so on?

If t$$anonymous$$s is not a snake game, you could still use the "numbers" to give each object some kind of offset so that they won't stay on top of each other.

Comment

People who like this

0 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 RaulG · Jan 14, 2015 at 11:00 PM 0
Share

Yeah, I was thinking of doing an Id system for each part.

I think the biggest issue is that Im trying to control multiple bodies with one script. It doesn't seem to like that very much.

avatar image

Answer by RaulG · Jan 15, 2015 at 12:58 AM

     public List<Vector3> posList = new List<Vector3>();
     
     public void Snaker()
     {
         posList.Add (eSpawn.eSnake [0].transform.position);
         posList.Add (eSpawn.eSnake [1].transform.position);
         posList.Add (eSpawn.eSnake [2].transform.position);
         posList.Add (eSpawn.eSnake [3].transform.position);
         posList.Add (eSpawn.eSnake [4].transform.position);
 
         Vector3 v0 = posList [0];
         Vector3 v1 = posList [1];
         Vector3 v2 = posList [2];
         Vector3 v3 = posList [3];
         Vector3 v4 = posList [4];
 
 
         float amplitudeY = 3.0f;
         float omegaY = 1.0f;
         timeI += Time.deltaTime;
         float y = Mathf.Abs(amplitudeY * Mathf.Sin(omegaY * timeI));
         
         eSpawn.eSnake [0].transform.position = new Vector3 (0.0f, v0.y + y, v0.z -1.0f * timeI);
         eSpawn.eSnake [1].transform.position = new Vector3 (0.0f, v1.y + y, v1.z -1.0f * timeI);
         eSpawn.eSnake [2].transform.position = new Vector3 (0.0f, v2.y + y, v2.z -1.0f * timeI);
         eSpawn.eSnake [3].transform.position = new Vector3 (0.0f, v3.y + y, v3.z -1.0f * timeI);
         eSpawn.eSnake [4].transform.position = new Vector3 (0.0f, v4.y + y, v4.z -1.0f * timeI);
         
 
     }


It's not the cleanest t$$anonymous$$ng ever. But I basically did what @NasarethMekuri said to do. Only problem is they all move the exact same.

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

28 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

Related Questions

What am I doing wrong with my AI? 1 Answer

Jerky motion using moveTowards at high speed 0 Answers

Enemy following Player on uneven surface 1 Answer

How can i set a radius for rotatearound ? 0 Answers

Time.deltaTime Too Variable 0 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