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 peteynunchucks · Jul 05, 2021 at 11:18 PM · collision2d gamemethods

Help with OnCollisionEnter method.

Hello,

I am trying to have the color of a ball change when it hits a wall in my game. The if statement I have for this works but when I try to have it change to a different color after it hits the right wall again this is where I have run into issues. When the code runs right now, anytime the ball contacts another surface including the paddles it changes color. So how should I proceed forward? Should I move the color-changing part of the code to its own method or is there a way to help it run independently from the other collision parts of the code in the method?

 void OnCollisionEnter2D(Collision2D collision)
     {
         if (collision.gameObject.name == "RacketLeft")
         {
             float y = hitFactor(transform.position, collision.transform.position, collision.collider.bounds.size.y);
 
             Vector2 dir = new Vector2(1, y).normalized;
 
             GetComponent<Rigidbody2D>().velocity = dir * speed;
         }
 
         if (collision.gameObject.name == "RacketRight")
         {
             float y = hitFactor(transform.position, collision.transform.position, collision.collider.bounds.size.y);
 
             Vector2 dir = new Vector2(1, y).normalized;
 
             GetComponent<Rigidbody2D>().velocity = dir * speed;
         }
         
         if (collision.gameObject.name == "Right Wall")
         {
             _renderer.sharedMaterial = materials[1];
         }
         else
         {
             _renderer.sharedMaterial = materials[0];
         }
     }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by rage_co · Jul 06, 2021 at 03:41 AM

You can use layers for this.... like

 if(collision.gameObject.layer == LayerMask.NameToLayer("wall"))
       { 
       }

just put this under the OnCollisionEnter2D method.......and then appropriately tag all the walls............then you can use this if tag to trigger any event you want.....you can make a similar if() and layer for the paddles

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

Answer by Devster2020 · Jul 06, 2021 at 10:05 AM

The problem is here:

 if (collision.gameObject.name == "Right Wall") {
     _renderer.sharedMaterial = materials[1];
 }
 else {
     _renderer.sharedMaterial = materials[0];
 }

using "else", make change the color of the ball for every collision that not be "Right Wall"..

If I understand correctly your idea, you need to change the code like this:

 if (collision.gameObject.name == "Right Wall") {
     _renderer.sharedMaterial = materials[1];
 }
 else if(collision.gameObject.name == "Left Wall"){
     _renderer.sharedMaterial = materials[0];
 }

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 peteynunchucks · Jul 07, 2021 at 04:11 AM 0
Share

I am trying to have the ball change color only when it hits the "Right Wall".

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

226 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 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

Ignore collision in specific circumstances while still using it as a trigger. 1 Answer

Trigger2D not working 0 Answers

Can I have two colliders attatched to my enemy that do different things and if so how?,Is it possible to have two colliders for one object 2 Answers

Collisions missing a frame... 1 Answer

How to limit OnCollisionStay calling rate? 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