- Home /
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.
first, after i hit the space button, the ball goes up but it won't deflect.
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?
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!
it works, but not so perfect... the ball still didn't reflect. did i assign the wrong tag on the walls? or something else?