I managed to make a “player” , make it move, jump, crouch , shoot.
Now I created an enemy and I want the enemy to shoot ( at player location first step).
I don’t know how do I reference my player location to the enemy . In the main scene, my player is an “Object” named let’s say Johnny. This is the sprite i use to move , shoot jump.
Now the question is , when I use a script which has something like this :
"GameObject.FindObjectOfType … " What do I have to input there to reference to Johnny?
If I simply type in there Johnny , it’s an unknown variable for the script to see.
target = GameObject.FindObjectOfType();
there is no component “Player” on the player (the components are SpriteRenderer, Animator, CharacterController2D, etc…), also the line can’t find Player on the player and target will stay “null”.
Then drag and drop the player in the inspector. Since it’s a public variable, I tend to stay away from using variable names that may exist on other scripts. (Such as player). Also for clarity purposes, it’s a good practice to name variables accordingly. @alex_apolzan
Anyway, since you now posses the player’s coordinates, you would only need to fire at the player’s current position. So maybe make a variable currentPosition = playerTrans.position; then have the enemy fire at that.
private Vector3 currentPosition;
// When player is detected
currentPosition = playerTrans.position;
// You wouldn't want to call this inside an update
// because it would constantly Update the player's
// position. You only want to assign the position
// upon detection