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
1
Question by mons7re · Jul 25, 2017 at 03:51 PM · collisionphysicsrigidbody

Can't count the hit bowling pins[physics in 3d]

Hi all, I am still new to Unity and trying to create my own bowling game. I have a bowling ball with a rigidbody attached to it and a bowling pin prefab with 10 pins attached to it (tagged pin). My floor is tagged "floor". Right now I have one script for the ball and I'm trying to create a second one, that should be attached to the pins which will check if they have been hit or not. I find this quite difficult even though I have the feeling it is not as complicated as I make it.

 public class Pins : MonoBehaviour {

        public int score;
            public int pinsNumber = 10;
        
            void OnCollisionEnter(Collision other)
        {
           if(other.gameObject.CompareTag("floor"))
           {
               for (int i = 0; i <= pinsNumber; i++)
               {

                   if(Vector3.Dot(transform.up, other.transform.up) < 0.9f)
                   {
                      // Increase score here
                         score+=1;
                         int total = score;
                         Debug.Log (total);
                   }
               }

           }


       }
     
 


     }


Basically, what this is doing now is the Console outputs increasing numbers if I hit the pins. Any help will be appreciated.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by nt314p · Jul 26, 2017 at 01:08 AM

Attach this script to your bowling ball:

 public class BowlingBall : MonoBehaviour {
     
     public bool collided = false;
     
     void OnCollisionEnter(Collision other){
         
         //checking if the bowling ball has collided with pins, not the floor
         if (other.gameObject.tag == "pin" && !collided){
             collided = true; // prevents the wait function from running multiple times
             StartCoroutine(Wait());
         }
     }
     
     public IEnumerator Wait(){
         // wait for 5 seconds (the f makes the number we enter a float)
         yield return new WaitForSeconds(5.0f);
         
         // looping through each pin prefab and calling the tallyScore function
         GameObject[] pins = FindObjectsOfType(typeof(GameObject)) as GameObject[];
         foreach (GameObject pin in pins) {
             pin.GetComponent<Pins> ().tallyScore();
         }
     }
 }

Attach this script to your pin prefab:

 public class Pins : MonoBehaviour {
     
     public static int score = 0;
 
     public void TallyScore(){
         
         //checking if the pin is standing up (if it's not, the pin's rotation will be not 0)
         if (transform.rotation.x < 1 && transform.rotation.z < 1){
             //pin is standing up (do nothing)
         } else {
             //pin is knocked over
             score++;
         }
     }
 }

This code basically checks if the bowling ball has hit a pin, waits 5 seconds, then checks if the pin is knocked over, and if it is, it adds one to the score.

Score is static so that each prefab doesn't have its own separate score, they all share the one variable.

If you have any questions, do not hesitate to ask!

EDIT: NEW PIN SCRIPT TESTING

  public class Pins : MonoBehaviour {
  
      void Update(){
          
          if (transform.rotation.x < 1 && transform.rotation.z < 1){
              Debug.Log("Pin Standing Up");
          } else {
              Debug.Log("Pin Knocked Over");
          }
      }
  }

Try attaching this script to your pin prefab and turning it over onto its side while you watch the console.

Make sure that the changes you make to the pin are applied to the prefab.

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 mons7re · Jul 26, 2017 at 03:42 PM 0
Share

Thank you very much for your comment and suggestions! I did as you told and attached the two scripts. However, I get a NullReferenceException in the first script that I attach to the ball. alt text NullReferenceException: Object reference not set to an instance of an object PinsFallen+c__Iterator0.$$anonymous$$oveNext () (at Assets/Scripts/PinsFallen.cs:26) UnityEngine.SetupCoroutine.Invoke$$anonymous$$oveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

I tried to modify it a bit and ins$$anonymous$$d of the line GameObject[] pins = FindObjectsOfType(typeof(GameObject)) as GameObject[];

I tried GameObject[] pins = GameObject.FindGameObjectsWithTag("pin") as GameObject[];

This resolved the issue with the nullreference. I added a Debug.Log(score) line to the Pins.cs, but the console doesn't output anything.

avatar image
0

Answer by mons7re · Jul 27, 2017 at 03:24 PM

Hello @n314p and thank you again.

I added the new script and now the whole time I have the message "Pin is Standing UP" . It doesn't change regardless of the position of the pins. I have attached it to the pin prefabs so all Pin objects have the script.

Also, the nUllreferenceexception error is still there. I think I am not initializing the pins correctly. This is my Ball script. using System.Collections.Generic; using UnityEngine;

public class BallMovement : MonoBehaviour {

 //force per one pixel
 public float powerPerPixel;
 public float maxPower;
 public float sensitivity;
     public bool collided = false;
 Vector3 touchPosition;
 bool isMoving;


 // Use this for initialization
 void Start () 
 {
     isMoving = false;

 }
 
 // Update is called once per frame
 void Update ()
 {
     if (!isMoving) 
     {
         if (Input.GetMouseButtonDown (0))
         {
             touchPosition = Input.mousePosition;
         }
         else if (Input.GetMouseButtonUp (0)) 
         {
             isMoving = true;
             transform.Rotate (new Vector3 (1, 5, 10) * Time.deltaTime);
             Vector3 releasePosition = Input.mousePosition;

             float swipeDistanceY = releasePosition.y - touchPosition.y;
             float power = swipeDistanceY * powerPerPixel;
             float swipeDistanceX = releasePosition.x - touchPosition.x;
             float throwDirection = swipeDistanceX * sensitivity;

             if (power < 0) {
                 power *= -1;
             }

             if (power > maxPower) {
                 power = maxPower;
             }

             GetComponent<Rigidbody> ().AddForce (new Vector3 (throwDirection, 0, power), ForceMode.Impulse);
         }

     }
             
 }
 

 void OnCollisionEnter(Collision other)
 {
     if (other.gameObject.tag == "pin" & !collided) {
         collided = true;
         StartCoroutine (Wait());
     }
 }

 public IEnumerator Wait()
 {
     yield return new WaitForSeconds (5.0f);

     GameObject[] pins = FindObjectsOfType (typeof(GameObject)) as GameObject[];
     //GameObject[] pins = GameObject.FindGameObjectsWithTag("pin") as GameObject[];
     foreach (GameObject pin in pins) {
         pin.GetComponent<Pins> ().TallyScore ();
     }
     Debug.Break ();
 }

}

Also, I noticed that when my ball collides with the pins, the z value stays higher than 1.

My pins script is the same as you suggested. using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Pins : MonoBehaviour {

 public static int score = 0;

 void Update()
 {
     if (transform.rotation.x < 1 && transform.rotation.z < 1) {
         Debug.Log ("Pin Standing Up");
     } else {
         Debug.Log ("Pin Knocked Over");
     }

 }

 public void TallyScore()
 {
     if (transform.rotation.x < 1 && transform.rotation.z < 1) {

     } else {
         score++;
     }
 }

}

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 nt314p · Aug 09, 2017 at 09:45 PM 0
Share

Not sure where the problem might be but if you want to decrease the sensitivity, change each 1 to a higher value, like 20.

avatar image
0

Answer by fani_2025 · Dec 04, 2017 at 04:38 PM

to calculate the bowling score accuratly, counting of bowling pins is essential in the game. go bowling alley

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

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

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

Related Questions

Keep Horizontal Momentum after Jump 2 Answers

Collision.impulse = 0 in OnCollisionStay Kinematic Static collision pair after Update 0 Answers

How to change CC script to Rigidbody script 1 Answer

Why is there no Rigidbody2D.SweepTest() ? 1 Answer

Handling colliders on hundreds of asteroids (not procedural asteroids) ...URGENT 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