Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by danmct1995 · Apr 22, 2020 at 09:55 AM · collisionunity 2drigidbody2dvelocityif-statements

Stuck on using if else to prevent ball from getting stuck in brick breaker. very new to unity & C# :(,How would I reference an object to add onto its velocity in 2D?

What I am trying to do is make a ball in a brick breaker game stop sticking on the vertical and horizontal axis. I want to execute this by adding to the balls velocity whenever it is between a speed of -1 and 1 by using adding a random range of velocity between -0.5 and 0.5.

How would I actually reference the ball and execute this in an if else statement?

I've tried several ways, but I'm too new to c# and unity to grasp the proper syntax or formula still.

I'm thinking from researching that I need to reference the rigidbody of the ball. I have the idea of what I want to accomplish in my head, but I'm not sure how to write it out. Would it be something like this? I'm not sure if I used too many if statement or used them incorrectly, but I'm hoping this is close to the solution.

      private void OnCollisionEnter2D(Collision2D collision)
         {
     
     If (Theball >= 1f ) {
     transform.Translate(vector2.right * Time.deltaTime);
     }
     If (Theball <= -1f) {
     transform.Translate(vector2.left * Time.deltaTime);
     }
     TheBall = get component<Rigidbody2D.velocity.x>() 
     }
     
     If (Theball >= 1f ) {
     transform.Translate(vector2.up * Time.deltaTime);
     }
     If (Theball <= -1f) {
     transform.Translate(vector2.down * Time.deltaTime);
     }
     TheBall = get component<Rigidbody2D.velocity.y>() 
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by streeetwalker · Apr 22, 2020 at 03:06 PM

HI @danmct1995, yes you need some help. I suggest you go through several getting started tutorials, and you can find a lot in YouTube and hone the Web. Start search with: Unity How to Reference a GameObject.

If your ball object is the the object the above script collides with, you can get it from the collider.gameObject.

  theBall = collision.gameObject;

do a google search on: Unity Collision object

(a word about coding style, do not begin variables identifier with a capital letter - only use a starting capital letter with class identifiers and function identifiers. Eg. use theBall not TheBall.)

If theBall is a GameObject, it cannot have a value of -1 or 1. You can test against the velocity of the ball's Rigidbody component, or it's transform.position, or many other properties theBall as a GameObject has.

You want to get the Rigidbody component of theBall? Google Unity how to get object components.

the general form is

   gameObject.GetComponent<ComponentIdentifier>();


note how all function identifiers begin with a capital letter! like OnCollionEnter, and GetComponent, and Translate

Keep pluging - you'll get there, and go through a lot of examples and tutorials!

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

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

Rigidbody not able to be linked to object within unity for some reason? 1 Answer

Rigidbody2D.Cast() against mutiple colliders with a single Rigidbody2D 0 Answers

how to control instances velocity? 0 Answers

Inconsistent Velocity? 0 Answers

How to have the same speed on Rigidbody2D in all resolutoins ?? 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