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
0
Question by BuzzardRaze765 · Aug 18, 2018 at 08:47 AM · charactercontrollervariablerigidbody2dvelocitymethod

Need help for my Arkanoid-like game. i got problem to make my ball reflects on the wall

i need help for my brick breaker game.

there was a problem on this section.

  1. first, after i hit the space button, the ball goes up but it won't deflect.

  2. second is...well, i've added it to the note section on my script:

    using System.Collections; using System.Collections.Generic; using UnityEngine;

          public class BallScript : MonoBehaviour {
             
                 public Rigidbody2D ballrb;
                 public float ballforce;
                 public string Wall;
             
                 // Use this for initialization
                 void Start () {
                     
                 }
                 
                 // Update is called once per frame
                 void Update () {
                     if(Input.GetKeyUp(KeyCode.Space)){
                         ballrb.AddForce(new Vector2(ballforce,ballforce));
                     }
             
                     if (ballrb.CompareTag (Wall) = "Vertikal") {//Error: The left-hand side of an assignment must be a variable, a property or an indexer
                         ballrb.velocity(new Vector2(ballforce,-1));//Error: Non-Invocable member of 'UnityEngine.Rigidbody2D.velocity' cannot be used like method
                     }
             
                     if (ballrb.CompareTag (Wall) = "Horizontal") {//Error: The left-hand side of an assignment must be a variable, a property or an indexer
                         ballrb.velocity(new Vector2(-1,ballforce));//Error: Non-Invocable member of 'UnityEngine.Rigidbody2D.velocity' cannot be used like method
                     }
                 }
             }
    
    

can anyone help me out with both of these problems?

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 rainChu · Aug 18, 2018 at 11:18 AM

First, we need to fix those errors you've pointed out in the code. After that happens, we'll see if the ball still won't deflect. This is a common beginner mistake, mixing up the == and the = operators. When you do the double ==, it means "Check if equal to." I like to read it aloud as "is." when you do a single =, it means "Set equal to." I like to read it aloud as "becomes."

Another issue with this code is not quoting strings. When you do this: ballrb.CompareTag( Wall )

You get an error, because Wall doesn't exist. You probably meant "Wall". However, this still doesn't fix the problem... Look at the documentation on CompareTag. It returns true or false, depending on whether the tag matched.

What you want to do is something more like this:

 if ( wall.CompareTag( "Vertical" ) ) {
     // Reverse direction
 }

In order to assign to wall, you should get a reference to it in OnCollision.

Another issue is using the velocity property as if it's a method. Instead of doing this:

 ballrb.velocity( new Vector2() ); // Wrong

You should do it like this: (See the manual on how to use velocity)

 ballrb.velocity = new Vector2(); // Right


These are the big issues with your code, and understanding the fundamentals of coding will go a long way. At this point, you should focus on learning these fundamentals, and recognizing what the compiler errors mean. Errors are there for a reason, and they often say, in english, what your problem is. For instance, the error "Non-Invocable member of ... cannot be used like a method" means exactly that.

If you learn what all the proper terminology means (property, method, member, invocable) then figuring out what went wrong will go a lot smoother.

Here's some code to put you on the right track: (Look up OnCollisionEnter event in the manual)

     void OnCollisionEnter( Collision collision )
     {
         // otherColider becomes the collider I hit
         var otherCollider = collision.collider;

         // If the other colider has "Vertical" tag...
         if ( otherCollider.CompareTag( "Vertical" ) )
         {
             // Take my current velocity
             var currentVelocity = ballrb.velocity;

             // Inverse the Y, but not the x
             currentVelocity.y = -currentVelocity.y;

             // Assign my new velocity
             ballrb.velocity = currentVelocity;
         }
     }

Try your best to solve these problems, and respond in a comment if you still need help. Good luck!

Comment
Add comment · Show 2 · 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 BuzzardRaze765 · Aug 19, 2018 at 11:26 AM 0
Share

O$$anonymous$$. i'm going to give it a try...

avatar image BuzzardRaze765 · Aug 20, 2018 at 12:42 PM 0
Share

it works, but not so perfect... the ball still didn't reflect. did i assign the wrong tag on the walls? or something else?

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

107 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

Related Questions

Make 2D movement more precise and less based off of forces and physics 2 Answers

Character jitter on collision 0 Answers

Unity C# 2D Adding Velocity on rotation 1 Answer

How can I get local 2d Velocity? 2 Answers

How to move a rigidbody2D at a constant speed without AddForce? 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