- Home /
Question is off-topic or not relevant
Finding the distance between objects
Hi guys, sorry I know this has been asked before, I've been following the advice on the other posts but it's just not working and I can't figure out why.
I'm trying to find the distance between the current object and the player, I've assigned "PlayerTag" in the tags section to my player object and I'm trying to get the distance between the two and output it to the console. Here's my code:
using UnityEngine;
using System.Collections;
public class EnemyController : MonoBehaviour {
public float hitpoints = 100f;
//Get player object
GameObject player;
float distance;
void awake()
{
player = GameObject.FindGameObjectWithTag("PlayerTag");
}
void update()
{
//get distance between player and zombie
distance = Vector3.Distance (gameObject.transform.position, player.transform.position);
//output distance to console [DEBUG]
Debug.Log ("Distance: " + distance);
}
public void RecieveDamage(float amt)
{
hitpoints -= amt;
if (hitpoints <= 0)
{
Die();
}
}
void Die()
{
Destroy (gameObject);
}
}
can you guys spot what's wrong? I'm sure it's something dumb but I've been looking at it for a while now and I just don't see it.
player = GameObject.FindGameObjectWithTag("PlayerTag").position;
Follow this Question
Related Questions
distance script work with nearest tags 0 Answers
Calling 2 objects into script for onCollisionEnter? 1 Answer
Same output_log.txt file being written to by multiple clients at once? 0 Answers
When I grab an object, it is too close 1 Answer
How to detect if two objects are are near eachother? 2 Answers