Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by unity_PkeTMQ-bDf-2XQ · May 27, 2020 at 09:47 AM · arrayarraysfollowfollow player

Making objects follow each other in a line, and objects ignoring minimum follow distance.

I'm trying to make a group of objects follow each other in a kind of conga line as they are collected by the player. The idea is that each object will set itself as the last object in the current line so that the next object knows who to follow. However, the following script has 2 problems.

  1. The objects do not follow each other in order, rather they only follow the player. I assume this is some sort of error with the static array and it's not being updated across all the other instances of the code, but I don't know how to fix it.

  2. The objects are ignoring the minimum distance at which they should stop moving towards the player. They continue to move into and push the player, which is odd because this same movement code worked before I started adding the array to make the conga line.

Would appreciate any help thanks!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class NewFollowCode : MonoBehaviour
 {
     public Transform thisRabbit;
     public Transform player;
     bool IsActive = false;
     float speed = 1.0f;
     const float minDistance = 2f;
     Vector3 DistancefromTarget;
     Animator m_Animator;
     public Transform target;
     public static Transform[] followers;
     public static int followerCount = 0;
     void Start()
     {
         followers[0] = player;
         m_Animator = GetComponent<Animator>();
     }
 
     private void OnTriggerEnter(Collider other)
     {
         if (other.transform == player)
         {
             IsActive = true;
             target = followers[followerCount];
             followerCount++;
             followers[followerCount] = thisRabbit;
         }
     }
 
     void FixedUpdate()
     {
         if (IsActive)
         {
             Follow();
         }
 
         DistancefromTarget = transform.position - player.position;
     }
 
     void Follow()
     {
         transform.LookAt(player);
         if (DistancefromTarget.magnitude > minDistance)
         {
             transform.Translate(0.0f, 0.0f, speed * Time.deltaTime);
         }
     }
 }






Comment
Add comment · Show 1
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 MrMinestrone · May 28, 2020 at 06:25 AM 0
Share

I have the same question.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by unity_PkeTMQ-bDf-2XQ · May 28, 2020 at 01:27 AM

Good News! I was able to fix the second problem. All I had to do was change void FixedUpdate to LateUpdate, and move the DistancefromTarget calculation from there to void FixedUpdate.

Like so.

 private void FixedUpdate()
 {
     DistancefromTarget = transform.position - player.position;
 }
 
 void LateUpdate()
 {
     if (IsActive)
     {
         Follow();
     }
 }

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

Answer by Bangurah · Nov 25, 2020 at 06:22 AM

Did you attach the script to the player or objects?

Also, the script keeps giving me NullException Errors for when: followers[0] = player;

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

Follow this Question

Answers Answers and Comments

146 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

Related Questions

Multidimensional array trouble 2 Answers

Prefabs instantiated from an array are keeping their public int value 1 Answer

Want to make GameObject follow another GameObject's path Unity3D 1 Answer

Problems with Arrays 2 Answers

IndexOutofRangeException 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