Quantcast
Channel: Questions in topic: "follow"
Viewing all articles
Browse latest Browse all 536

How to make player go to and eat all food objects on the screen

$
0
0
I've been trying to make a 2D game recently where the player moves around the screen and eat all the food objects that appear. This means he moves to the closest food object, eats it, and then move on to the next until none is left. I found code someone used for the following or moving towards the food but this is specific to one object in the game. Only the one selected object is the target for the player. See below script(#C): using UnityEngine; using System.Collections; public class FollowScript : MonoBehaviour { public Transform target; public float speed = 2f; private float minDistance = 0.3f; private float range; // Use this for initialization void Start () { } // Update is called once per frame void Update () { range = Vector2.Distance(transform.position, target.position); if (range > minDistance) { Debug.Log(range); transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime); } } } This is what the script looks like on the player. Here is the place where you drag and drop the "target" which is only one object. See below: ![alt text][1] [1]: /storage/temp/85417-capture.jpg In my other script I use OnTriggerEnter to Destroy the food gameObject once they touch but then the game stops. My Question is, how would I make the script to not only move to one specific target, but to one after another when one gets eaten? I was thinking that to make the player follow the food by a "tag" but how would I set this tag in the "FollowScript"?

Viewing all articles
Browse latest Browse all 536

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>