Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by phillipe · Dec 14, 2012 at 05:14 PM · gameobjectsceneloadlevel

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.

Comment
Add comment · Show 12
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image luniac · Dec 16, 2012 at 02:20 AM 1
Share

so youre saying that when the player is not moving the enemy doesnt touch the player?

avatar image clunk47 · Dec 16, 2012 at 02:22 AM 1
Share

@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.

avatar image luniac · Dec 16, 2012 at 02:27 AM 2
Share

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???

avatar image clunk47 · Dec 16, 2012 at 02:35 AM 2
Share

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.

avatar image clunk47 · Dec 16, 2012 at 03:34 AM 2
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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);
     }
 }
 
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image phillipe · Dec 16, 2012 at 03:36 AM 2
Share

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).

avatar image phillipe · Dec 16, 2012 at 03:45 AM 1
Share

Sure man. Thanks a lot.

avatar image luniac · Dec 16, 2012 at 03:59 AM 2
Share

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...

avatar image clunk47 · Dec 16, 2012 at 03:59 AM 2
Share

There you go sir.

avatar image phillipe · Dec 16, 2012 at 04:04 AM 2
Share

Wow. Now, this problem is case closed! Thanks for you two.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges