- Home /
Issue with enemies going towards player
I'm having a little issue with my enemies at the moment, I want my coloured balls to roll towards the player, but at the moment, they just seem to roll to the position in the photo, regardless of where my player is.
Does anyone know why this may be? Here is the code I'm using:
using UnityEngine;
using System.Collections;
public class moveTowardsPlayer : MonoBehaviour
{
public Transform target; // drag the player here
float speed = 10.0f; // move speed
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
}
That's odd, I won't lie. At first glance I can't see anything wrong with the script. Could you do some debugging? Place a Debug.Log("Player position: " + transform.position) on the player and a Debug.Log("Target position: " + target.position) on the enemy and make sure that indeed these two values are the same.
Or you could try and use Vector3.Lerp ins$$anonymous$$d of $$anonymous$$oveTowards and see if that helps. Let me know the results of the debugging :)
I tried the Debugs there and indeed something is wrong as the x position of the target is -15 :
Why would that be the case do you know? I'll try Vector3.Lerp now
I suppose Lerp won't work either in this case. For testing purpose, can you pass a GameObject as target ins$$anonymous$$d of a transform, and use then Target.transform for the debugging? It's fishy, but we need to restrict the circle :)
Edit: Oh wait! Does the cube have a parent object? Like is the hierarchy "Player -> Cube" and you're passing one of these as a transform and they're not aligned? $$anonymous$$aybe the Cube is parented to a gameobject but the pivots (the return values of the transform.position) are offsetted? Could you check that?
Ok yes, that could be the problem indeed. A workaround would be to get the player reference in the Start function of the enemies. You would do like this:
using UnityEngine;
using System.Collections;
public class moveTowardsPlayer : $$anonymous$$onoBehaviour
{
public Transform target; // drag the player here
void Start()
{
target = GameObject.FindGameObjectWithTag("Player").transform;
}
float speed = 10.0f; // move speed
void Update()
{
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, target.position, speed * Time.deltaTime);
}
}
And then make sure that the cube has the tag "Player". That way, at the start of the scene, all the enemies will look for a Player and store that as a reference ins$$anonymous$$d of the prefab.
Brilliant, works great :) Thanks very much for your help Alessio. If you convert your comment to an answer, I will accept it :)
Answer by Alessio89 · Aug 17, 2014 at 01:39 PM
Since I suppose the question is answered, I'll post the complete answer here for anyone who might have the same problem:
The problem was that the enemies were referencing a Prefab and not the instanciated gameobject.
The solution was to attach the tag "Player" to the cube and then make sure the enemies were referncing it at the beginning of the scene like so:
using UnityEngine;
using System.Collections;
public class moveTowardsPlayer : MonoBehaviour
{
public Transform target; // drag the player here
void Start()
{
target = GameObject.FindGameObjectWithTag("Player").transform;
}
float speed = 10.0f; // move speed
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613160812im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
How to move gameobject (main camera) to another gameobject (UI camera) position ? 1 Answer
MoveTowards sends object passed target 1 Answer
Having a 3D text appear in the middle of the screen without using GUI (C#) 2 Answers
Make player follow mouse position 3 Answers