An enemy follow a player on exact path

Please help me with a script for an sprite follow another sprite, i searched on google and i found a lot of scripts, but the sprite that is supposed to follow isn’t moving.

ok let me tell you about my project, i have two sprites.
Sprite A and Sprite B. Sprite A is the Player and Sprite B is the Enemy

i have a controller on sprite A with some inputs, “W” for up, “S” for down, and the sprite is moving forward continuous.
i want that sprite B following sprite A on X axis, and if the players hits up or down, the sprite B should follow the exact path of the Sprite A

let’s say there is an obstacle and Sprite A goes up and then down, Sprite B will follow the exact path.

this is the controller for sprite A:

using UnityEngine;
using System.Collections;

public class Miscare : MonoBehaviour {
public Sprite playerSprite;
public float speed = 1f;
public float moveForce = 2.0f;
private Vector3 moveDirection = Vector3.zero;
public float forwardMovementSpeed = 3.0f;
private float NewSpeed;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

if (Input.GetKey (KeyCode.W))
transform.position += new Vector3 (0.0f, speed * Time.deltaTime, 0.0f);
if (Input.GetKey (KeyCode.S))
transform.position -= new Vector3 (0.0f, speed * Time.deltaTime, 0.0f);
//Feed moveDirection with input.
moveDirection = new Vector3(Input.GetAxis(“Horizontal”), 0, Input.GetAxis(“Vertical”));
moveDirection = transform.TransformDirection(moveDirection);
//Multiply it by speed.
moveDirection = speed;
//Making the character move
transform.Translate(Vector3.forward * Input.GetAxis(“Vertical”) * Time.deltaTime
speed);
Vector2 newVelocity = rigidbody.velocity;
newVelocity.x = NewSpeed;
rigidbody.velocity = newVelocity;
NewSpeed = forwardMovementSpeed + speed;

}

}

Sorry for my bad English. if you want to now more please ask me.

Thank you,
Andrei

A possible solution would be to keep a list of positions and add the current position in the update method. Then you could use this list to set the position of the second player.
You could also save the animation states so that the second player will walk and not slide over the floor.

But then agian, I’d say a more elegant solution would be to save the position of the first player every 0.5 seconds, then build a script for the second player that move towards this position. That way, the animations will always be correct.

Finally, I heard something about pathfinding in unity, you could google it and see if that’s what you’re looking for.

Does any of this help?

thank you, i wil try this method, and i will come back to tell you how it was.