- Home /
Breakout style game. Avoid player controlled bumper to go out of the game walls
Hi everyone, i've started to look at unity just a week ago, and i totally loves it! I'm watching tutorials and trying to learn as much as i can, but as always i think that the best way to learn something is by trying to use it =)
So i've decided to create a very simple game to test some of the features of the engine and make some scripts. Guess what...a Breakout style game (what an original idea...)
My problem (...i know, most probably a silly one) is that the user controlled bumper can "pierce" the area walls if the player keep pressing the left or right arrow.
The area walls are two modified cubes (static colliders with a bouncy physic material attached). The bumper is a capsule (non-kinematic rigidbody with constraints on all axes except the x).
The script to enable the movement of the bumper is, in it's skeletal version, something like this:
if (Input.GetButton("Horizontal"))
{
float h = Input.GetAxis("Horizontal");
float xPos = 0;
xPos = h * paddleSpeed;
rigidbody.MovePosition(rigidbody.position + new Vector3(xPos, 0, 0) * Time.deltaTime);
}
I've made different kind of prototypes to avoid this problem...all faulty except one:
Limit the movement of the bumper to a max x (-x) position by checking the position of the bumper before applying the code posted before. It works, but it should be carefully used a predefined value to limit the movement...too simple to fall in errors.
On collisionEnter disable the movement by using a boolean flag, on collisionExit reenable it. The only problem was to make the bumper exit correctly from the collision, by getting the normal of the collision points. But sometimes this normal points toward the z axes (not the x) and this cause the bumper to "fly" above the ground
Using two box collider (one on the left, one on the right of the bumper) to check which one have collided, and so move the bumper correspondingly. I've failed in doing that because it seems that i can't send a message to the "parent" of the colliders to signal the collisions.
Well...long story short...as i'm a total noob, i would like to know if someone have a best practice/advice/hint in how to resolve this kind of problems. Many thanks in advance to all.
Another simple way you can stop your bumper at walls is to just remember the maximum and $$anonymous$$imum position the bumper is able to travel. Then you don't have to check for collisions, the input function just stops to work in a specific direction as soon as $$anonymous$$X or maxX is reached respectively.
If you keep having problems, you could also check PushyPixels youtube channel. iirc, he did a very extensive tutorial on breakout and a couple of other basic games which were pretty good.
Answer by Elroyman · Aug 11, 2013 at 12:22 PM
Rather than MovePosition, you can use AddForce.
rigidbody.AddForce(new Vector3(xPos, 0, 0)*Time.deltaTime;
Then it will pay attention to other rigidbodies (so add a couple to the walls) without any extra code. With the Time.deltaTime at the end there, you can put this in you Update() method, but its recommended to put physics stuff (i.e. forces and motion of nonkinematic objects) in a FixedUpdate() instead.
xPos effectively becomes the 'strength' of the force.
Perfect! That did the trick! Now i'm having fun by applying different types of forces (Impulse or velocity changes) and working with the drag.
$$anonymous$$any thanks!
Answer by spectre1989 · Aug 11, 2013 at 12:23 PM
Firstly make sure you're applying changes to your RigidBody in FixedUpdate, that's where all physics-affected code should be placed. You totally could already be doing this, I just can't tell from the snippet so thought I'd mention.
To stop your bumper from tunnelling through the walls, try changing the collision detection mode on the bumper's rigidbody from "discrete" to "continuous" or "continuous dynamic". I don't entirely understand the difference between those two modes as I haven't used them myself.
If neither of those work, then you could use Physics.Raycast to cast a ray out from each side of the bumper and see what it hits?
Thanks for the reply, using the continuous or continuous dynamic doesn't avoid my problem (most probably for my errors =>). Anyway i've discovered something new by reading the documentation of the detection mode. Thanks!
Your answer
Follow this Question
Related Questions
move camera when it collides with a trigger 1 Answer
Moving a player smoothly without breaking collisions? 2 Answers
Why doesn't my playerobject collide with other objects? 0 Answers
Player object falling through game environment, collision not detected with ground 0 Answers
Moving a Simple Ball [Main Player!] inside a tunnel without gravity! 1 Answer