I’m making a 2d game and I am stuck at a part where the player needs to escort 1 of the secondary characters across a level, I have been trying but I’m stuck, I’m new at Unity and scripting.
What I’m exactly trying to accomplish
When the player “bumps” into the secondary character, a small message above the secondary character’s head pops and he starts to follow wherever the main character goes.
Any help will be greatly appreciated, thank you for taking your time to read this post!
Yes this is really interesting. Is it top down or side 2D? I’m also working on a 2D game - a sidescroller metroidvania- and I imagine this mechanic might be useful. The only proper way to achieve this is probably to record the players moves and replay them on the following character with a fixed time offset.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class FollowTest : MonoBehaviour
{
public GameObject ObjectToFollow;
public float TimeOffset = 1;
public int Granularity = 10;
public float InterpolationSpeed = 10;
private Queue<Vector3> path = new Queue<Vector3>();
private float queueTimer;
private Vector3 currentPathPosition;
protected void Update ()
{
if(currentPathPosition != Vector3.zero)
transform.position += (currentPathPosition - transform.position) * Time.deltaTime * InterpolationSpeed;
}
protected void FixedUpdate()
{
if (ObjectToFollow == null)
return;
queueTimer -= Time.fixedDeltaTime;
if (queueTimer <= 0) {
queueTimer = TimeOffset / Granularity;
if(path.Count == Granularity)
currentPathPosition = path.Dequeue();
path.Enqueue(ObjectToFollow.transform.position);
}
}
}
You need to assign the ObjectToFollow GO in the editor or code (in your case, when you bump into the other character)
If you raise the TimeOffset, you’ll obviously also need to raise the recording Granularity
(Edit: had a mistake in fixedupdate - is fixed now)
Oh and you can replace the Queues vector3 type with a struct type containing all the info you need to have your follower mimic your player. (ie adding rotation, or which animation it should play)
Yes. A generic like the used queue works as a container for any datatype. You can utilize this to pack more useful data into a single element of your queue by making your own datatype.
Say you want to mimic not only the position, but also the rotation and if the character is jumping at this point (just like in the donkey kong video). You can save all that data in the queue like this:
private struct Playbackdata
{
public Vector3 position;
public Quaternion rotation;
public bool jumping;
}
private Queue<Playbackdata> playbackQueue = new Queue<Playbackdata>();
Yeah, that’s true, although I just attach this script to the secondary character, that’s it? Sorry, I’m really new to this and I’d rather ask to be sure that I did not failed a step or did something wrong, thank you so much for helping me!
Sure. Add the monobehavior to your second character and set the ObjectToFollow to be your player gameobject (drag and drop in the editor).
This is just a starting point though.
You will want to set the ObjectToFollow variable when the player collides with the secondary.
You will also need to stop him to follow when your goal is reached.
This is gamespecific behavior and therefore I didnt include it