Dog chasing my player on curved path?

Hey,
I want a gameobject (dog) to constatnly follow my player from behind. (just like in subway surfer). But in my case the path is nor straight neither player is constantly running. The player can turn at right angles and player runs only when use uses its keyboard.

I have looked into iTween examples but in those the path are predefined, i do not want that in my case.
How do i do it?

P.S. I want my dog to follow exactly the footsteps of my player. If my player jumps then he jumps, etc… But if my player stops the dog comes running towards him, eat him and the game ends.

The typical suggestion/solution is to have the player save its position at fixed interval in FixedUpdate(), InvokeRepeating(), or a Coroutine. Then the following object would use a fixed speed following algorithm to follow the object. The problem I have with this solution is that jumps will likely look funny. So I came up with an alternative. Instead of set speed, the dog follows the exact same positions but at a slower rate. Three other things to note:

  • Note that the player does not insert points if the distance between the current point and the previous point is below some threshold. Since no points are inserted during stops, the dog will not stop.
  • The player is given a 3 second head start.
  • For my test, I did not have any collider on the dog or the player. For your chase, this is workable if you use some minimum distance as “being caught” rather than a collision.
  • If you need collisions, you’ll have to turn the collider off on the dog while he gives the player a head start, plus you likely want to use Rigidbody.MovePosition() to move the dog, rather than a direct assignment to the transform.position.

Player SavePath script:

#pragma strict

import System.Collections.Generic;

public var path : Queue.<Vector3> = new Queue.<Vector3>();
public var threshold = 0.01;

private var prevPoint : Vector3;

function Start () {
	InvokeRepeating("SavePoint", 0.0, .03333); 
}

function SavePoint() {
	if ((transform.position - prevPoint).magnitude > threshold) {
		path.Enqueue(transform.position);
		prevPoint = transform.position;
	}
}

Dog FollowPath script:

#pragma strict

import System.Collections.Generic;

private var path : Queue.<Vector3>;

function Start () {
	renderer.enabled = false;
	yield WaitForSeconds(3.0);
	renderer.enabled = true;
	var sp = GameObject.Find("Player").GetComponent(SavePath);
	path = sp.path;
	transform.position = sp.transform.position;
	InvokeRepeating("FollowPoint", 0.0, .04);
}

function FollowPoint () {
	if (path.Count > 0) {
		transform.position = path.Dequeue();
	}
}

Here are the c# scripts for anyone who needs it:

Save Path script:

using UnityEngine;
using System.Collections.Generic;

public class SavePath : MonoBehaviour {
	public Queue<Vector3> path= new Queue<Vector3>();
	public float threshold = 0.01f;
	
	private Vector3 prevPoint;
	
	void Start () {
		InvokeRepeating("SavePoint", 0.0f, 0.03333f); 
	}
	
	void SavePoint() {
		if ((transform.position - prevPoint).magnitude > threshold) {
			path.Enqueue(transform.position);
			prevPoint = transform.position;
		}
	}
}

Follow Path script:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class FollowPath : MonoBehaviour {
	
	private Queue<Vector3> path;
	
	IEnumerator Start () {
		yield return new WaitForSeconds(3);
		SavePath sp = GameObject.Find("Player").GetComponent<SavePath>();
		path = sp.path;
		transform.position = sp.transform.position;
		InvokeRepeating("FollowPoint", 0.0f, 0.04f);
	}
	
	void FollowPoint () {
		if (path.Count > 0) {
			transform.position = path.Dequeue();
		}
	}
}