• 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 WaqarKhan703 · Sep 12, 2013 at 05:35 AM · prefabs

How to check if any of the prefab position went beneath the particular constraint (good coding habits - Static variable usage)

Hi , I'm trying to build a small 2d game in which there is a bucket that catches the balls. If any of the ball went beneath the bucket game should end. As I'm very new to the unity3d and scripting, I couldn't figure out how to check this that if any of the ball go below a particular y position. Balls are dynamically created using prefab.I'm using this script for ball creation and is attached to my MainCamera

 using UnityEngine;
 using System.Collections;
 
 public class ballCreationScript : MonoBehaviour {
 
     public GameObject ballPrefab;
     public Vector3 positions;
     
     // Use this for initialization
     void Start () {
 
         StartCoroutine(balls());
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     IEnumerator balls() {
         while (true) {
             positions = new Vector3(Random.Range(-2.5f, 2f), 20, 0);
             Instantiate(ballPrefab , positions , Quaternion.identity);
             
             yield return new WaitForSeconds(1f);
             
         }
     }
 
     private object WaitForSeconds(float p)
     {
         throw new System.NotImplementedException();
     }
 }

And for ball movement i'm using this script and is attached to Balls prefab

using UnityEngine;

using System.Collections;

 public class BallMovementScript : MonoBehaviour
 {
 
     public static float speed = -8;
     public static float ballYPosition;
     GameObject ball;
 
 
     // Use this for initialization
     void Start()
     {
 
         rigidbody.velocity = new Vector3(0, speed, 0);
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (ball.gameObject.tag == "Ball")
         {
             if (ball.gameObject.transform.position.y < -4)
             {
                 print("not Good");
             }
         }
 
     }
 
 }
 ![alt text][1]

This is the main Screen . For now I just placed paddle instead of Bucket. balls would come from above and if any of the ball misses the paddle game should end.

Below script is attached to my paddle. When collisions occurs it destroy that object.

 using UnityEngine;
 using System.Collections;
 
 public class PaddleScript : MonoBehaviour {
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hitInfo;
         if (Input.GetMouseButton(0))
         {
             if (Physics.Raycast(rayOrigin, out hitInfo))
             {
                 transform.position = new Vector3(hitInfo.point.x, -1.993791f, 0);
            
             }
         }
     }
     void OnCollisionEnter(Collision col) {
         Destroy(col.collider.gameObject);
     }
 
 }
 

I hope it clears my Point.

[1]: /storage/temp/15371-screen1.png

Comment
Add comment · Show 4
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 vexe · Sep 12, 2013 at 11:51 AM 0
Share

The image you provided wasn't very helpful actually. I don't see any balls or buckets. You sure this is the right image? - if so, could you provide a 3d in-scene image? (like the one I provided) - That'd be great.

avatar image vexe · Sep 12, 2013 at 11:54 AM 0
Share

This is really strange, why do I see "0 answers" even though my answer is there? - Also the question icon is red from the questions index, strange... Has any moderator seen this?

avatar image WaqarKhan703 · Sep 12, 2013 at 12:30 PM 0
Share

Hey Vexe, I can't figure what's the problem I'm trying to attach images but couldn't make it. Well I hope from the paddleScript may be you can get what i'm trying to make.

avatar image vexe · Sep 12, 2013 at 12:32 PM 0
Share

Here's how - I will check you script.

alt text

attachpic.png (7.7 kB)

1 Reply

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

Answer by vexe · Sep 12, 2013 at 06:34 AM

OK, first welcome and hello :)

About what you're trying to do, you don't need to perform any checks about the balls' y coordinate. You said there's a bucket, right? - just like a basketball game - that bucket should have a collider (box collider is ok) within it, make sure to check 'is trigger' on the collider, which means it won't intercept / physically collider with whatever comes through it - in other words, anything could pass right through it. Now whenever a ball gets into the bucket, it collides with that collider, and since you ticked 'is trigger', OnTriggerEnter gets 'fired' (OnTriggerEnter is an event - when you hear an event got 'fired' it means the event has happened or occurred, if you like) - Now, you will 'handle' this event in your ball script, like:

 OnTriggerEnter(Collider other)
 {
   if (other.gameObject.tag == Tags.bucket)
   {
      Debug.Log ("ball " + id + " is inside bucket"); // assuming you have an id for your balls, which could be a name, a number, etc
   }
 }

You don't have to handle it inside the ball, you can do it in the bucket script if you like:

 OnTriggerEnter(Collider other)
 {
   if (other.gameObject.tag == Tags.ball)
      // do something...
 }

This is from a basketball tutorial I went through when I was staring (I don't wanna give you links to, because their code quality just SUCKS!)

alt text

Note that, in my case, I have to make some checks to make sure that the ball goes in the basket from top to bottom, and not the opposite. But since you're using a bucket you don't have to because it's sealed from the bottom.

Now, you mentioned good habits? - here's a few:

  • NEVER use statics unless you know what you're doing. When you make a variable static it means that ALL instances/objects of your class will share/have the same variable. When to use this? - let's say you wanted to make a ball id (integer), each time you instantiate a ball, you give it a new id (assuming that you don't destroy them, they will keep their id unique) - how would you do that?

A simple way is using static variable to count how many balls do we have, use the value of that counter as our id, like so:

 public class Ball
 {
 
     private static int counter = 0;
     public int Id { private set; get; }  // this is a property, anybody can 'get' its value, but only inside this class, that its value can be 'set'
 
     void Start()
     {
        Id = counter++;
     }
 }

This is OK, it makes sense for all balls to have the same counter, right? :) With the previous code, first ball gets instantiated will have an Id of 0, 2nd one will have 1, then 2, 3, 4, etc. This is because counter is static, it will remain the same between all ball instances. BUT, why didn't I make Id static? o.O - Ask yourself: "Should all balls have the same Id?" - Of course not! otherwise it's not an identifier that I could identify a ball with - it's like you're saying all the balls are the same :D

This is exactly what you did with:

  public static float speed = -8;
  public static float ballYPosition;

That way, all balls have the same speed and ballYPosition :/

Another good example of statics: It's always best to organize your tags in your game, keep things in one place and not scattered around. Because what if you changed your tag? - you would have to go and change it in numerous places. The way I like to do it, is to use a "Tags" class - It's a class that I always like to use to store all my tags. A Tags sample class would be:

 public abstract class Tags
 {
     public static string bucket = "Bucket"; // you can use static instead of const
     public static string ball   = "Ball";
     public static string player = "Player";
 }

(abstract means that you can't make an instance of your class = you can't instantiate objects of that class - this is good for our purposes because we are intending to access the members of the class in a static manner)

And then you can access your tags in a static manner as we mentioned, like so:

 if (collider.tag == Tags.player)
 {
   // do something
 }

You don't need an instance of the class to access a static variable, just nameOfClass.myStaticVar; - Which makes sense. There is a bit more about static variables, I will leave learning their basics and googling to you, but for example. More about usages of static variables within Unity (great example!).

  • Give your variables self-explanatory non-confusing names. Like your positions variable. A rule of thumb: Always try to make your variables identifiable by their names. By identifiable I mean someone can easily tell what they serve and what's their purpose. And sometimes, good naming could lead to the discovery of the data type of the variable. On the contrary bad naming will lead to confusion. For me when I saw positions I immediately thought it's either a list or an array of Vector3.

There's a lot that I would love to cover for you :) - But I think it's best to learn from some good tutorials. Please understand that there are a TON of shitty ugly disgusting crappy tutorials EVERYWHERE! - Just make sure you choose the good ones, ones that are tutored by 'good' programmers, who write good quality good, there are a lot of course.

  • Programming-related: LEARN OOP! And get your basics together just right.

  • Unity-related: I have a few, Quilly is very friendly to new-comers and explains things just right! (basic-intermidate, uses C#) - Then you got BurgZergArcade, great programmer! Got an extensive 400-videos RPG series! (Basic, intermediate, advanced, uses C#) You Got UnityGems which I mentioned earlier (a bit more advanced stuff). You can't go wrong with these guys, anything they say is like sacred! :D (Intermidate-advanced, uses C#) Then you got Brackeys (Basic-intermidate, not sure about lang) and Etiskee (Basic-intermidate, uses JS) for FPS-related stuff. Great, but not very technical. Finally thewalkerboys (Basic, uses JS) and thetornadotwins (Basic, not sure about lang)

Lastly, what are you doing with:

 private object WaitForSeconds(float p)
 {
    throw new System.NotImplementedException();
 }

WaitForSeconds is already implemented for you, unless you don't want to make something like, WaitForRealSeconds I don't think you need to bother yourself with re-implementing it :)


basketball.png (172.9 kB)
Comment
Add comment · Show 6 · 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 WaqarKhan703 · Sep 12, 2013 at 08:49 AM 1
Share

Hi Vexe , Thank you so much for such an amazingly explained answer and tutorials you've mentioned. You mentioned WaitForSeconds() that I've implemented down there, to be honest I didn't figure it out yet what it's doing for me. As I'm using Visual Studio 12 as an IDE for scripting, it was popping an error at YiedInstruction sentence. So from suggestion I've selected one of the solution :D . I know it's a bad approach for learning, but I thought it's ok to proceed for now. Again Vexe thank you so much for your Precious Time :)

avatar image vexe · Sep 12, 2013 at 08:51 AM 0
Share

Anytime :) - I Salute you for choosing C# and not JS! If you thought my answer was helpful, please mark it as correct. Thanks, and good luck :)

avatar image WaqarKhan703 · Sep 12, 2013 at 08:53 AM 0
Share

And what about if ball doesn't collide with the bucket box collider, and ball goes below the bucket I wanted to show a Dialog or some GUI thing to pop up and say you've failed the level. Please $$anonymous$$gest how to check that.

avatar image vexe · Sep 12, 2013 at 09:00 AM 0
Share

The collider should be inside your bucket - Your bucket as well, should have a collider attached to it. So you got two colliders: first one is inside the bucket used to tell if a ball is inside the bucket or not - call it bucketInnerCollider or something. The 2nd covers the bucket itself, preferably a mesh collider which takes the shape of the bucket - Component | Physics | $$anonymous$$esh Collider. - Call it bucketOuterCollider (It's not a very good habit to always use mesh colliders, use them when it's a must)

Can you show visual feedback to what you're doing? - A screen shot of the buckets/balls and what you're doing would be great! - Take a shot, edit your question and insert it as an image.

avatar image TechSupportIncoming1 · Oct 10, 2013 at 12:11 PM 1
Share

Thanks dude.. Your thoughtful and meaningful is surely helpful..$$anonymous$$ade my day!

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.

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

17 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

Related Questions

Prefab Spawning With SpawnPoints (Loot System) 0 Answers

PreFabs not colliding with other GameObjecs, but collide with themselves. 0 Answers

How to fix the problem of object instantiating partially? 0 Answers

Line from Parent to Children on UI 0 Answers

How would I go about storing spawned prefabs to acces them ? 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