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
0
Question by gsaccone101 · Jul 17, 2013 at 06:20 PM · playersceneenemyhit

How can I load a scene when the enemy hits the player?

Here's my script. Is there a way to start a new scene start as soon as the enemy hits the player?

    var target : Transform;    
     var lookAtDistance = 20.0;
     var attackRange = 15.0;
     var moveSpeed = 20.0;
     var damping = 6.0;
     private var isItAttacking = false;
  
     function Update () 
     {
     distance = Vector3.Distance(target.position, transform.position);
  
     if(distance < lookAtDistance)
     {
     isItAttacking = false;
     renderer.material.color = Color.white;
     lookAt ();
     }   
     if(distance > lookAtDistance)
     {
     renderer.material.color = Color.white; 
     }
     if(distance < attackRange)
     {
     attack ();
     }
     if(isItAttacking)
     {
     renderer.material.color = Color.red;
     }
 }
  
 function lookAt ()
 {
 var rotation = Quaternion.LookRotation(target.position - transform.position);
 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
 }
  
 function attack ()
 {
     isItAttacking = true;
     renderer.material.color = Color.red;
     transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime); 
 }

Is there a way to make a new scene start as soon as the enemy hits the player?

Comment
Add comment
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

5 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Graham-Dunnett · Jul 17, 2013 at 09:01 PM

Yes. Use collision detection to determine when the enemy hits the player, and use Application.loadLevel to change scene.

Comment
Add comment · Show 1 · 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 gsaccone101 · Jul 17, 2013 at 10:53 PM 0
Share

Okay. How does collision detection work? Do I need a script or is it a component?

avatar image
0

Answer by Ejpj123 · Apr 02, 2016 at 01:00 AM

@gsaccone101 Collision Detection is a Component. Add the componnet on what you want to not go through. I suggest, for 2D OR 3D, use the component Mesh Collider.

Comment
Add comment · Show 2 · 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 Le-Pampelmuse · Apr 02, 2016 at 01:04 AM 1
Share

There is no Collision Detection Component. What you are referring to are Colliders.

Using the $$anonymous$$esh Collider for every 2D or 3D collision is not a very good suggestion, because depending on the cmesh complexity, they have way more impact on performance than standard primitive collider shapes like Box, Sphere, Capsule, etc..

avatar image Ejpj123 Le-Pampelmuse · Apr 02, 2016 at 08:27 PM 0
Share

Oh, that makes much more sense.

avatar image
0

Answer by DroidifyDevs · Apr 02, 2016 at 01:56 AM

Just use OnTriggerEnter to detect the collision, then use Application.LoadLevel to load another scene. Read the docs for those 2, they are basics of Unity and very easy but essential to use.

Comment
Add comment · 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
0

Answer by TX Manager · Apr 02, 2016 at 10:35 PM

It's easy follow this steps :-

 void OnCollisionEneter (collision other)
 {
 if (other.colider.tag == "Enemy Shot")
 {
 SceneManager.LoadScene ("Scene Name Or Index");
 }
 }

don't forget to write using UnityEngine.SceneManagement; in the up of the script if you are using unity 5.3 if not, use Application.LoadLevel ("Scene Name Or Index");

Comment
Add comment · 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
0

Answer by TCROC · Apr 02, 2016 at 10:37 PM

 var target : Transform;    
      var lookAtDistance = 20.0;
      var attackRange = 15.0;
      var moveSpeed = 20.0;
      var damping = 6.0;
      private var isItAttacking = false;
 
 private int currentLevel;
 
 function Start () {
 // This is the current scene you are in and have built in build settings
          currentLevel = Application.loadedLevel;
 }
   
      function Update () 
      {
      distance = Vector3.Distance(target.position, transform.position);
   
      if(distance < lookAtDistance)
      {
      isItAttacking = false;
      renderer.material.color = Color.white;
      lookAt ();
      }   
      if(distance > lookAtDistance)
      {
      renderer.material.color = Color.white; 
      }
      if(distance < attackRange)
      {
      attack ();
      }
      if(isItAttacking)
      {
      renderer.material.color = Color.red;
      }
  }
   
  function lookAt ()
  {
  var rotation = Quaternion.LookRotation(target.position - transform.position);
  transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
  }
   
  function attack ()
  {
      isItAttacking = true;
      renderer.material.color = Color.red;
      transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime); 
 
 // This will reload the scene you are currently on.  If you would like to load the next scene you would say Application.LoadLevel (currentLevel + 1);
 Application.LoadLevel (currentLevel);
  }
Comment
Add comment · Show 1 · 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 TCROC · Apr 02, 2016 at 10:35 PM 0
Share

"private int currentLevel" should be "var currentLevel". $$anonymous$$y bad I'm used to C# :)

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

enemy targeting player in different scene 1 Answer

Kill the Player? 1 Answer

Enemy targeting player in different scene 1 Answer

Collision Detection Issue with SetActive(false) not working 2 Answers

auto select enemys 3 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