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 anurooppv · Oct 28, 2014 at 04:40 AM ·

Code in OnCollisionEnter?

alt textAttached is my game scene. The user can have maximum of 5 collisions with the obstacles. So I would like to know what must be the code when the ball hits the obstacle? I mean the code for continuing the game with ball moving forward(transform.translate).

Below code is currently present in OnCollisionEnter function in BallController program:

transform.position = new Vector3( transfomr.position.x, transform.position.y - 0.1f, transform.position.z)

transform.Translate (Time.deltaTime*2, 0, 0);

rest of the code is for other tasks like reducing the no of balls remaining etc.

This is working fine. But sometimes after collisions, the ball going in an entirely different direction & finally it exits the scene slowly. I don't know what is the reason behind it. Am I missing something? This is the first time I am developing a code in Unity.

1.jpg (275.2 kB)
Comment
Add comment · Show 5
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 anurooppv · Oct 28, 2014 at 04:38 AM 0
Share

I have updated the question with more details

avatar image _Yash_ · Oct 28, 2014 at 05:55 AM 0
Share

you are moving object 0.1 downward when it collides. so if it collides with a box below it, it keeps calling OnCollisionEnter. I think that might be causing it. try debugging position when it collides and after you move object.

avatar image anurooppv · Oct 28, 2014 at 06:25 AM 0
Share

The gap between the obstacles are enough to prevent collision immediately after a collision. I tried playing the game after removing 0.1f. Sometimes ball is going somewhere slowly out of game scene after few collisions.

avatar image KyleIchiNi · Oct 28, 2014 at 06:53 AM 1
Share

You should always have the ball ricochet towards the center of the level rather than a set down vector. That way it will always bounce away from other blocks rather than getting stuck in corners. Also, if the ball is moving slowly off screen after a certain number of collisions, check the debug console to make sure you're not getting any null reference exceptions. If you get them during game play, the script will crash but the game will continue, this means whatever the ball was doing last, it will continue doing. So if it crashes while moving a certain direction, it will likely continue on in that direction.

Conversely, it might be better to use a rigidbody in this instance ins$$anonymous$$d since it already has collision and ricochet built in. This way you could also apply a force to the ball should it start moving backwards in order to continue it on the correct path.

avatar image apollo25 · Oct 28, 2014 at 07:13 AM 0
Share

use on trigger enter...dont use collisions and when chances left is zero then stop its motion

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by felixpk · Oct 28, 2014 at 05:47 PM

The problem is that you are not checking whether the collision was fired because you hit a top or a bottom block. You could for example:

  • set the tag of the topBlocks to "topBlock"

  • set the tag of the bottomBlocks to "bottomBlock"

    void OnTriggerEnter2D(Collider2D other) { if (other.tag == "topBlock") { transform.position = new Vector3( transform.position.x, transform.position.y - 0.1f, transform.position.z); // & reduce lifes and so on } else if (other.tag == "bottomBlock") { transform.position = new Vector3(transform.position.x, transform.position.y + 0.1f, transform.position.z); // & reduce lifes and so on } }

And the most important: post your code and post it formated right!

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I hide/destroy certain parts of an object? 1 Answer

Timeline/preview of the game scene. 0 Answers

Multiple cameras in game scene to create preview of the game 0 Answers

Weird bug i encountered with 2 screens. Any help? 1 Answer

Ball travelling in wrong path after collision 0 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