• 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 vibhu123 · Jun 28, 2014 at 05:36 PM · pingpong

want the both balls to pass y axis and then trigger game over

I'm trying to make an ping pong style game so in level 2 i have added 2 balls and i want to trigger game over only when both the balls pass beyond y axis 3.5 ::"

i'm using javascript

this is the script i'm using for the balls

"function Update () { if(transform.position.y < -3.5) { mainGameScript.GameOver(); } }"

and the main game script

"

unction GameOver() { if(score > PlayerPrefs.GetInt("High Score" )) { PlayerPrefs.SetInt("High Score" , score); }

  Application.LoadLevel("Game_Over");

} "

this is the whole script for the ball

"var mainGameScript : MainGame; var particles_splash : GameObject;

function Awake() { rigidbody.AddForce(4, 4, 0, ForceMode.Impulse); InvokeRepeating("IncreaseBallVelocity", 2, 2);

 }
 
 
 function Update ()

{ if(transform.position.y < -3.5) { mainGameScript.GameOver(); } }

function IncreaseBallVelocity () { if(rigidbody.velocity.x < 14 && rigidbody.velocity.y < 14 && rigidbody.velocity.x > -14 && rigidbody.velocity.y > -14);

{ rigidbody.velocity *= 1.05; Debug.Log("velocity: " + rigidbody.velocity);

} }

function OnCollisionEnter(collision : Collision) { Instantiate(particles_splash , transform.position , transform.rotation); audio.Play(); }"

and this is the whole script for maingame

" var score3DText : TextMesh; private var score : int = 0;

function Awake () { InvokeRepeating("UpdateScore" , 0.05, 0.05); }

function UpdateScore() { score += 1; score3DText.text = "Score : " + score.ToString(); }

//this is called from BonuAreaDetectionBox.js inside OnTriggerEnter() function AddScoreForBonusArea () { score += 50; }

function GameOver() { if(score > PlayerPrefs.GetInt("High Score" )) { PlayerPrefs.SetInt("High Score" , score); }

  Application.LoadLevel("Game_Over");

}"

Comment
Add comment · Show 1
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 Juice-Tin · Jun 28, 2014 at 06:36 PM 0
Share

I don't see anything to answer... You should revise your question, it's hard to understand and possibly incomplete.

1 Reply

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

Answer by Taschenschieber · Jun 28, 2014 at 06:35 PM

 function Update () { if(transform.position.y < -3.5) { mainGameScript.GameOver(); } }


This means GameOver will be called whenever one of the two balls is out of the game, so you need to add a check for the other ball in there. You could give all balls a tag "Ball" and then do a check by using GameObject.findGameObjectsByTag("Ball") and then checking for each ball if it is still in the game area in a loop.

I could give you some code, but only in C#, dunno if that would help you.

Comment
Add comment · Show 3 · 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 vibhu123 · Jun 28, 2014 at 07:00 PM 0
Share

thanks for d response Taschenschieber. i'm a beginner & this worked fine for me

so i had another Q. it's that can i access a java script from a C# script if yes plz give me some code to modify this thank u

avatar image Taschenschieber · Jun 28, 2014 at 07:14 PM 0
Share
 void Update(){
   bool gameOver = false;
   foreach(GameObject go in GameObject.findGameObjectsWithTag("Ball")){
     if(go.GetComponent<Transform>().position.y < -3.5){
       gameOver = true;
       break;
     }
   }
   if(gameOver)
     mainGameScript.GameOver();
 }

It would be best to attach this script to the Camera or to only one of the balls, because if you attach it to both balls, it will be run twice per frame, but with identical results - so it would be wasting CPU time. And yes, referencing a JS script from a C# script should be perfectly possible, haven't tried it yet, though.

avatar image vibhu123 · Jun 28, 2014 at 07:24 PM 1
Share

thanks Taschenschieber. this solved it completely

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

smoother pingpong? 1 Answer

Mathf.Pingpong without the repetition!? 1 Answer

Object hits another but doesnt bounce off again. 0 Answers

Where is Ping Pong mode in Animation Window? 3 Answers

C# Mathf.PingPong Rotate Back and Forth 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