- Home /
Load a scene/level when enemy is close...
Hi everyone! I found this script (below) and attached it to an enemy who is following the player and once they collide, a level will load (in this case, I created a Death scene)
var levelToLoad : String;
function OnTriggerEnter(hit : Collider)
{
Application.LoadLevel(levelToLoad);
}
The script actually worked, but I just realized it's not working when my character is not moving which let the enemy revolve around my character until I start to walk then the level changes.
What I want to achieve though is something that would load a level once my enemy game object catches my character even in idle mode. Is it possible to load a scene through distances of game objects? If not, how can I load a scene with my character in idle animation? Really need some script for this...Thanks guys.
so youre saying that when the player is not moving the enemy doesnt touch the player?
@luniac yeah, idk why but I have read the docs in the past and for some reason a character controller only reacts to collisions when movement is being called.
so why not just write a script that takes a reference of the player and subtract the players position from enemy position. If the resultant vector.sqrmagnitude < some number depending on the size of the player, then the death scene will load.... simple???
true, you could also do float distance = new Vector2(player.transform.position, enemy.transform.position); float killrange = 3.0f; if(distance < killrange).... Something along those lines that wouldn't include collision at all. @luniac +1 for the suggestion.
Ok removed my old answers just to clean up a bit. I have posted a new answer with a script based on distance from player to enemy. This is just to get you started, you need to adjust things to fit your needs.
Answer by clunk47 · Dec 16, 2012 at 03:23 AM
Here is the code I posted earlier, but for multiple enemies. Just be sure to tag each enemy with tag "enemy".
var levelToLoad : String;
private var enemies : Array;
private var player;
var distance : float;
var killRange : float = 5.0;
function Start()
{
player = transform;
enemies = GameObject.FindGameObjectsWithTag("enemy");
}
function Update()
{
enemies = GameObject.FindGameObjectsWithTag("enemy");
for(var enemy : GameObject in enemies)
{
distance = Vector3.Distance(player.position, enemy.transform.position);
if(distance <= killRange)
Application.LoadLevel(levelToLoad);
}
}
Hurrah! $$anonymous$$an, finally it works. Yeah, I have multiple enemies... But this script works for my prototype. $$anonymous$$y final game has multiple enemies with the same name and components (merely clones to each other).
or you could adjust the script and attach it to the enemy, since theres only one player, you dont have to worry about more then 1. It kind of makes sense too since its the enemy attacking the player after all??
not that it matters of course... both ways work...
Wow. Now, this problem is case closed! Thanks for you two.
Your answer
Follow this Question
Related Questions
Multiple Instances of My Current Scene? 0 Answers
Setting variable as type "Scene" 4 Answers
The best way to load scenes ingame 3 Answers
How to see what level is running? 2 Answers
Loading a level on contact 2 Answers