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 smems · Mar 27, 2015 at 05:30 PM · raycastscorecolission

Increase score on Raycast hit.

Hello :) I'm kinda new to unity and I couldn't figure out a way to solve this. I also searched a bit in this forum and didn't find a fix to my problem.

My problem is, I use a Raycast to detect when the Player hits the top of my other GameObject(Cube). This Raycast decision is inside my Update void which means this will run every frame. I only want my score to increment by 1 for every hit on the Cube, not for every frame it is sitting on the Cube.

My code: (C#)

 void Update () {
     
             RaycastHit hit;
             //detect if player has hit the top of the platform
             if(Physics.Raycast(transform.position, Vector3.down, out hit, 0.6f))
             {
                 if(hit.collider.tag == "Platform") //i check if i hit the specific Cube
                 {
     
                     print ("HIT!!!!!!!!!");
                     isAir = false;
                     //PlatformMovement.platformMovement.shouldMove = false;
                     GameController.controller.score += 1; // i increase my score here
                 }
             }
     }

Any help would be appreciated :D Thank you

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 smems · Mar 27, 2015 at 09:50 PM 0
Share

Thank you for the answer. :D It did the job ;)

1 Reply

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

Answer by Priyanshu · Mar 27, 2015 at 07:05 PM

These type of situations can be solved by using bool. I tried to explain it in comments

     public RaycastHit hit; //Assign hit here so it doesn't get assigned every frame.
 
     public bool hitOnce = false;    //Bool to update score once.
 
     void Update () {
     
         Debug.DrawRay(transform.position, Vector3.down * 0.6f, Color.red);

         //detect if player has hit the top of the platform
         if(Physics.Raycast(transform.position, Vector3.down, out hit, 0.6f))
         {
             //i check if i hit the specific Cube and it has not been hit yet.
             if(hit.collider.name == "Platform" && !hitOnce ) 
             {
                 print ("HIT!!!!!!!!!");
                 //Change 'hitOnce' to 'true' so that 'if' statement is not executed every frame while player is sitting on the Cube.'
                 hitOnce = true;
                 isAir = false;
                 //PlatformMovement.platformMovement.shouldMove = false;
                 GameController.controller.score += 1; // i increase my score here
             }
         }
         //Change 'hitOnce' back to false when player moves away from Cube. That is 'RayCast' return false.
         else hitOnce = false;
     }
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Questions about Is Triggers and player collision. HELP! 2 Answers

3D Jump Colission Detection 1 Answer

How to increase score on hit 0 Answers

Make an Enemy Shoot through Raycast 1 Answer

change object material on mouse click. 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