Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by prateekkp · Aug 05, 2014 at 07:13 AM · itweenfollowpathchase

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Aug 05, 2014 at 07:15 AM 0
Share

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'.

avatar image prateekkp · Aug 07, 2014 at 06:42 AM 0
Share

hey robertbu, i have updated my question.

avatar image Mayank Ghanshala · Aug 07, 2014 at 08:08 AM 0
Share

is it a 3d game?

avatar image CrilleStyles · Aug 07, 2014 at 08:33 AM 0
Share

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;
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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();
     }
 }



Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image prateekkp · Aug 07, 2014 at 04:43 PM 0
Share

i need to convert this in C# and see whether it works or not.

avatar image prateekkp · Aug 12, 2014 at 08:42 PM 0
Share

it worked. the dog movement is jittery though. i will find a way to solve it. thanks alot man. guys like u are inspiration.

avatar image
0

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();
         }
     }
 }
 




Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges