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 /
  • Help Room /
avatar image
0
Question by Blankdud · Mar 18, 2016 at 06:33 PM · 2dscripting problemanimatorspritesmovetowards

Trying to simulate SNES party snake, non-grid.

For practise for and on my own game, I am working on a snake-type party following script. This would look similar to Chrono Trigger or Earthbound, and similar to but not exactly like an SNES Final Fantasy, because the latter uses a rigid grid movement whereas the other is largely free-form.

In my PlayerManager script (which is attached to the leader of the party, movement works great. I get a Vector2 for movement based on the GetAxisRaw Horizontal and Vertical methods, and move the character with:

 rigidBody2D.MovePosition(rig.position + (movement * speed) * Time.deltaTime)  


In my FollowerManager script I have the following variables to track the leader and his movements:

 public GameObject leader;
 public Queue<Vector2> leaderTrail;
 bool followLeader;
 public Vector2 lastLeaderPosition;


On paper, my goal here is that when the leaderTrail Queue gets so big, the follower will begin to follow them on the exact same trail. leaderTrail will not increase in size if the leader doesn't move. I'm using the following code for this:

 //If leader doesn't move, do not add to trail
 if (lastLeaderPosition != (Vector2)leader.transform.position)
     leaderTrail.Enqueue(leader.transform.position);

 //When leader trail gets sizable, follow them
 if (leaderTrail.Count > 20)
     followLeader = true;
 else
     followLeader = false;
 
 if (followLeader)
 {
     //Check if follower is in the same position as the first spot in the leader's trail
     if ((Vector2)transform.position != leaderTrail.Peek())
     {
         //Move follower
         transform.position =  Vector2.MoveTowards(transform.position, leaderTrail.Peek(), Time.deltaTime * speed);
     }
     else 
     {
         //If follower is in trail position, remove it from Queue
         leaderTrail.Dequeue();
     }

     //Update leaders last position
     lastLeaderPosition = leader.transform.position;
 }


A couple issues I'm running into:

  1. My controlled leader character moves far faster than my follower, despite having the same speed set. I realize it's because of what I'm doing with speed and Time.deltaTime, but I have no idea how to move the rigidBody2D at the same rate as simply moving the transform.

  2. I don't know how to set the animator on the follower. I had originally typed out a bunch of IF statements dictating the 0's and 1's required to make the blend tree work based on the MoveTowards vector, but it looked gross and felt even grosser. (It's a simple 2D Freeform Cartesian blendtree) Is there a better way to do that based on my code?

  3. I'm a total newbie, so hints, tips and advice outside of my two issues are always welcome and encouraged. Any help is appreciated.

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

0 Replies

· Add your reply
  • Sort: 

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

76 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

Related Questions

Issue: deactivated animated sprites reappear and go crazy on animation state transition 0 Answers

Help with 8-DOM Weapon Sprite Issue 0 Answers

HELP,Watched tutorial https://www.youtube.com/watch?v=Xnyb2f6Qqzg 1 Answer

Drawing Sprite between 2 points at runtime 0 Answers

Animating Sprites Through Separate Script? 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