- Home /
How do I let my Enemy get the Player's Current Position and other variables?
So usually in games Enemies need to know where the player is.
That would require me to in an Enemy Script to get the player's curernt position constantly.
In java that's easy.
In the Enemies class you make a Player p;
and then in the constructor you set p = the player given in the constructor list
and then in update() theplayerX = p.getPlayerX();
But i know it doesn't work that way in Unity C#. How do i do it?
I'm raedy to start my game and that's one of the basic things i need to do. Get players variables or Get Objects positions
Like other things i would need to know is: =Enemy should know if Player is punching him or if player is running away or if players energy level is low
You know things like that
How many scripts do you have and what are the names for each of them?
We need to know that first because there are different ways of doing it depending upon your setup.
Taryndactyl well. For my simple AI so far, i would just have 1 for the enemy. One script. In my initial stage of making the AI for the enemy i just want him to be walking around the city and if he sees me he can decide to chase me and attack me or not.
So yeah basically 1 script for now.
For The Enemy it would be EnemyAI
And then for the player I import the FPS script and the mouseLook script so that can look around ike in traditional FPS games.
Then i also want objects like boxes or brick walls or cars to respond to actions from the player or enemies(like for example being pushed or destroyed) so i guess i would make one Script for that.
Answer by SnowkK · Sep 03, 2013 at 05:35 PM
if what you want is player position on c# you can just use this code
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public GameObject player;
public vector3 playerpos; //you need to set the player on the inspector
void update()
{
playerpos = player.position;
}
}
Great. so if i set the players position in the inspector, that means the initial position right? cause i mean my player will be moving and so the position will be changing.
And also, that's the position. How about other things like STATES. Like in my Player i will switch to blocking animation if the player blocks. and upon this i will set the state to BLOC$$anonymous$$ING.
so the enemy just needs to get that state variable the same way you told me? So i should make the player's STATE variable public and set it in the inspector?
Your answer
![](https://koobas.hobune.stream/wayback/20220613113521im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make the level screen????? 0 Answers
Error BCE0077 1 Answer
How do I Reset My Variables On GameOver? 1 Answer
getting udp package info inside unity (GlovePIE) 0 Answers
Help with scriptableobjects 1 Answer