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:
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.
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?
I'm a total newbie, so hints, tips and advice outside of my two issues are always welcome and encouraged. Any help is appreciated.