Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 $$anonymous$$ · Oct 20, 2017 at 07:44 PM · end

How do I end my game?

I am making a game where you play as an aliean spaceship and you have to avoid homing rockets, I want that if you hit the rocket you wait about 3 seconds and then the whole game stops, I want the program to close. I have been trying this for 2 days and I can't figure it out, I tried it myself and I used the internet but it won't work.

This is the code for the rocket: using System.Collections; using System.Collections.Generic; using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))] public class HomingMissle : MonoBehaviour {

 public Transform target;

 public float speed = 7f;
 public float rotateSpeed = 200f;

 public GameObject explosionEffect;

 private Rigidbody2D rb;

 // Use this for initialization
 void Start () {
     rb = GetComponent<Rigidbody2D>();
 }
 
 void FixedUpdate () {
     Vector2 direction = (Vector2)target.position - rb.position;

     direction.Normalize();

     float rotateAmount = Vector3.Cross(direction, transform.up).z;

     rb.angularVelocity = -rotateAmount * rotateSpeed;

     rb.velocity = transform.up * speed;
 }

 void OnTriggerEnter2D ()
 {
     Instantiate(explosionEffect, transform.position, transform.rotation);
     Destroy(gameObject);
     Application.Quit();
 }

}

And this is the code for the player/alien spaceship: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerController : MonoBehaviour {

 public float moveSpeed;
 
 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f )
     {
         transform.Translate (new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
     }

     if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
     {
         transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
     }

 }

}

Please help me, if you help me I will mention you in the game credits

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by finlay_morrison · Oct 20, 2017 at 07:57 PM

Firstly Application.Quit dosen't work in the unity editor and it onl works on some builds, it is generally frowned upon to use application.quit. However if you really want it to quit after 3 seconds here is how you do it

private void QuitAfterDelay(float time) {

     Invoke("QuitApplication", time);

}

private void QuitApplication() {

     Application.Quit();

}

Whenever you want to quit just call the QuitAfterDelay() function and inside the brackets put a float of how long you want to wait.

Hope this is what you wanted!

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 $$anonymous$$ · Oct 21, 2017 at 07:45 AM 0
Share

Thanks for your help but I get some errors, i'm trying to fix it.

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

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

Related Questions

Any Suggestions To Hide Terrain Borders? 3 Answers

Finishing game script c# 2 Answers

Animation syncronization trouble 1 Answer

Detect when animation starts/ends via script 1 Answer

I can't get a collision to cause the end of my game 1 Answer


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