- Home /
enemy targeting player in different scene
Hi.How do i make enemy from another scene to target the player when he goes on other scene ? (The enemies on level 2 Will target the player when the player Will be on level 2 )... I tried this but I've got a null reference exception
var Distance;
var Target : GameObject;
var lookAtDistance = 25.0;
var chaseRange = 15.0;
var attackRange = 1.5;
var moveSpeed = 5.0;
var Damping = 6.0;
var attackRepeatTime = 1;
var TheDammage = 40;
private var attackTime : float;
var controller : CharacterController;
var gravity : float = 20.0;
private var MoveDirection : Vector3 = Vector3.zero;
function Start ()
{
attackTime = Time.time;
target = GameObject.Find("Health");
}
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
lookAt();
}
if (Distance > lookAtDistance)
{
GetComponent.<Renderer>().material.color = Color.green;
}
if (Distance < attackRange)
{
attack();
}
else if (Distance < chaseRange)
{
chase ();
}
}
function lookAt ()
{
GetComponent.<Renderer>().material.color = Color.yellow;
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function chase ()
{
GetComponent.<Animation>().Play("run");
GetComponent.<Animation>()["run"].speed = 1;
GetComponent.<Renderer>().material.color = Color.red;
moveDirection = transform.forward;
moveDirection *= moveSpeed;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
function attack ()
{
if (Time.time > attackTime)
{
GetComponent.<Animation>().Play("attack");
GetComponent.<Animation>()["attack"].speed = 1;
Target.SendMessage("ApplyDammage", TheDammage);
// Debug.Log("The Enemy Has Attacked");
attackTime = Time.time + attackRepeatTime;
}
}
function ApplyDammage ()
{
chaseRange += 30;
moveSpeed += 2;
lookAtDistance += 40;
}
Can someone help me please Thank you !
@Hellium's absolutely right - this makes no sense. How can an enemy on level 2 target the player who's on level 1?
I dont want to run two scenes at the same time . There is the first scene and if the player enter in a specific area ,scene 2 is load . When scene 2 is load , the enemies in scene 2 cant find the target "Health" because we cant have an other first person controller in the second scene . it´s why I'm trying to change this script .
The enemies on level 2 Will target the player when the player Will be on level 2 ...
In the Start function, the first person controller should have been instantiated, so does the "Health" game object. How and where do you instantiate these objects ?
"The enemies on level 2 Will target the player when the player Will be on level 2 ..."
Then, the title and the question must be changed.
Answer by puppeteer · Jul 31, 2015 at 01:54 PM
I think you should give the enemy script a way of knowing who the player is.
In the enemy script:
Define a variable that will hold a reference to the player object:
public string playerTag = "Player";
internal Transform playerObject;
And in the enemy's Start() or Awake() functions add this line:
playerObject = GameObject.FindGameObjectWithTag(playerTag);
That should give all enemies a reference to the player in any scene. Now you can make the enemy attach playerObject.
Your answer
Follow this Question
Related Questions
Enemy targeting player in different scene 1 Answer
Kill the Player? 1 Answer
Enemy does not look the player 2D platformer. 1 Answer
How can I load a scene when the enemy hits the player? 5 Answers
Enemy spawn then select target? 1 Answer