- Home /
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.
I think you need to provide more details. Assu$$anonymous$$g there are objects that the player and the dog need to move around, two choices come to $$anonymous$$d:
AI where the dog finds its way to the player around obstacles.
Having the dog follow exactly in the players 'footsteps'.
You can use Nav$$anonymous$$esh if that's what you need. I got the AI follow code here
var target : Transform;
function Update()
{
GetComponent(Nav$$anonymous$$eshAgent).destination = target.position;
}
Answer by robertbu · Aug 07, 2014 at 08:00 AM
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();
}
}
i need to convert this in C# and see whether it works or not.
it worked. the dog movement is jittery though. i will find a way to solve it. thanks alot man. guys like u are inspiration.
Answer by oronbz · Sep 28, 2015 at 06:28 AM
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();
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613155834im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Follow curved path with dynamic speed 2 Answers
ITween orienttopath x-y-z? 1 Answer
iTween makes a weird start on the path 1 Answer
Game object follow path of other game object 2 Answers
Need help with physics 1 Answer