- Home /
Vector3.distance is always returning 0
Hello, Im trying to make an enemy follow my player by flying towards him al the time. And I want to use vector3.distance to make the enemy stop a short distance before the player. But it keeps returning 0,0,0 on both my player and enemy gameobjects. Here is my code so far.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySkullMovement : MonoBehaviour {
public Vector3 target;
public float speed;
private float distance;
private Vector3 player;
private Vector3 enemy;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform.position;
enemy = GameObject.FindGameObjectWithTag("Enemy").transform.position;
}
void Update() {
Look();
distance = Vector3.Distance(enemy, player);
target = GameObject.FindGameObjectWithTag("Player").transform.position;
float step = speed * Time.deltaTime;
print(distance);
Debug.Log(player);
Debug.Log(enemy);
Debug.Log(distance);
if(distance > 1 && distance != 0)
{
transform.position = Vector3.MoveTowards(transform.position, target, step);
}
}
void Look(){
transform.LookAt(GameObject.FindWithTag("Player").transform);
}
}
Any help will be greatly apprreciated.
Answer by lmoro · Jan 15, 2018 at 10:44 AM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySkullMovement : MonoBehaviour {
public float speed;
private float distance;
private Transform target;
void Start()
{
target = GameObject.FindGameObjectWithTag("Player").transform;
// the enemy is this GameObject so access to "transform" means access to enemy.transform
}
void Update() {
Look();
distance = Vector3.Distance(transform.position, player.position);
float step = speed * Time.deltaTime;
print(distance);
Debug.Log(target.position);
Debug.Log(transform);
Debug.Log(distance);
if(distance > 1.0f && distance != 0)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
void Look(){
transform.LookAt(target);
}
}
Try this.
As you may have noticed I used Transform ins$$anonymous$$d of Vector3
That's because Transform is assigned by reference so you don't have to find its value every frame.
Ins$$anonymous$$d you were reading the position value at the Start() but never updating it, that's why you had that problem, you were never updating your values.
As final consideration: try to avoid using FindGameObjectWithTag in Update() it's computing expensive, as you can see I got the reference to the player on Start() and never updated it.
Answer by henrimh · Jan 15, 2018 at 10:43 AM
You seem to compare "enemy" and "player" values in Vector3.Distance().
--- But you never update them! --- You only update "target.position".
Answer by imrankhanswati · Jan 15, 2018 at 10:58 AM
Hi @Jaapster1337:
To me the script is looking fine but my doubt is that the "Player" or "Enemy " or both of them is null so look to console and check whats the the result of the Debug statement is, if its not showing any thing or there is any error about null reference then its means that player or enemy is null.
and i will suggest not to use the "FindGameObjectWithTag" due to sometime we forget to tag our object in game or some time spelling mistake happens when tagging object and also why to wast little CPU power to find the game object instead we can cache these objects.
Answer by ransomink · Jan 17, 2018 at 03:16 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySkullMovement : MonoBehaviour
{
public float speed;
private float distance;
private float step;
private Transform t;
private Transform target;
void Start()
{
t = transform;
target = GameObject.FindGameObjectWithTag( "Player" ).transform;
}
void Update()
{
Look();
CheckDistance();
step = speed * Time.deltaTime;
// Distance exist?
if ( distance != 0 && distance > 1 )
{
t.position = Vector3.MoveTowards( t.position, target.position, step );
}
}
void Look()
{
t.LookAt( target );
}
void CheckDistance()
{
distance = Vector3.Distance( t.position, target.position );
Debug.Log( distance );
}
}