Unity C# Bounce off Wall - c#

So I'm making a 3D pool game. Basically I have a main camera that when you press a button it adds force to the cue ball based on the position of the camera, which works ok.
But whenever the cue ball hits the wall of the table, it just stops. I want it to smoothly bounce off the wall like a real pool cue ball would.
The cue ball is just a basic sphere Game Object. The walls are basic cubes with colliders.
I have tried Vector3.Reflect with no success. It seems to bounce back a tiny bit but then immediately stops.
Any help would be great!

You should create a PhysicMaterial with low or no friction (both dynamic and static), bounciness = 1 and Bounce Combine = Maximum and then apply that PhysicMaterial to the rigidbody of your sphere

You can do one thing.
Store the ball velocity when it collide with the wall, calculate the reflected direction through Vector3.Reflect and give the stored velocity to the ball in reflected direction.
Hope that this will help you...
Best,
Hardik.

To make bounce on wall we need to create physic material and after that we need to change the value of dynamic friction =0.3 and static friction =
0.3 and bounciness=0.8 and frictionCombine choose Average from dropdown and bouncecombine choose Average .
Hence the ball is start bouncing on the wall by using the upper property.

Related

How to simulate running on Wall like The Flash in unity

Please is it possible to make a Player with rigidbody or character controller run on wall just like The Flash. Please guide me on how to implement this.Here's a link of what I'm talking about. Thank you in advance.
Rotate the player to be vertical to the building, adjust the gravity parameter on your rigid body (make it lower so the added force won't be canceled by it) and just add the force (or set the velocity, whatever makes you happy)
I'm going to assume you already know how to move the player around on the ground. All you need to do is create some form of detection for when they want to begin running on the wall (maybe the player presses a button, or walks up to the wall), at which point you will want to do 2 things: rotate the player to have their feet on the wall, and change gravity to be pulling the player into the wall.
If we assume the player is facing the wall when they go up on it, you can just rotate 90 degrees backwards. Changing gravity should be easy too. Once the player is fully rotated, do something like this
Vector3 newGravityVector = tranform.down.normalized
Physics.gravity = transform.down.normalized * 9.8 //9.8 is the default gravitaty value, feel free to use a different multiplier if you wish.
(this script should be attached to your player)

How can i make a rigidbody speed up everytime it collides with a wall at an angle?

I am trying to make a game where i got a ball with an arrow rotating around it. The ball starts as immovable, but when i hit a button i launch it to the direction the arrow is pointing at. Player will have the option mid air to press another button to make the ball "sticky" so when it hits a wall it will stick to the surface and repeat the process till he gets to the top of the level.
Aside from that, i want to give the option to the player to not stick to the wall if he doesn't press that button and instead bounce off the wall but when he does that, the player should speed up with every bounce, giving the option to either play it safe and slow or try to go fast getting more points as he does.
For the early prototyping i used force to move the player up every time he launches but i am not sure how i can make him speed up every time he bounces off the wall. It feels to me like a math problem more than it is a coding challenge. What i am thinking is that i have to find the angle on which player hits the wall, and add force according to that towards the direction he is supposed to follow after the collision.
Sadly i am not that good with trigonometry (working on it though). I am thinking that i might need to use a formula containing sin, cos, tan formulas but i m not sure how to do it. Any help is much appriciated! If you need more information on it please tell me and i ll be happy to provide.
Edit: After the first reply to this question i also found out those links that dive deeper into the subject. I m gona link them here for people that have the same issue.
Bouncing a ball off a wall with arbitrary angle?
http://www.3dkingdoms.com/weekly/weekly.php?a=2
If your ball has velocity vector V=(vx,vy), then after bouncing from standing surface with normal N, ball's new velocity is
V' = V - 2 * N * (V.dot.N)
where dot is scalar product of vectors (vx*nx+vy*ny)
Particular cases:
bouncing from vertical wall (N=(+-1, 0)) causes reversing of vx-component, vy component remains the same. V' = (-vx, vy)
bouncing from horizontal wall (N=(0, +-1)) causes reversing of vy-component, vx component remains the same. V' = (vx, -vy)
Note I recommend to work in velocity vector components, and use angles only when they are really needed.
If you need to calculate bouncing from moving bat, it is worth to change stationary coordinate system to moving one, connected with the bat, find reflection in that system, and revert to stationary system.
Ok so problem was solved!
I want to thank both #MBo and #shingo for their contribution to the solution. While the answer of Mbo solved the problem via trigonometry and gave me nice material to study and figure out how things work, i followed shingo's advice in the comments of my question and managed to do it without diving deep into math.
So basically what he said, and what i did, was to use Unity's physics engine and let the ball hit the wall. After the ball bounces off the wall, it gets a new Velocity vector towards the direction it would go based on physics. I then created an OnColllisionExit check, and when the ball stops colliding with the wall, i AddForce to it to the direction of its new Velocity Vector3.
Works like a charm!
Thank you all for your contributions!

How to make ball bounce

I have a game where u bounce a ball with platform and u have to destroy all boxes but my code for bounceing ball its not working like I want.
It works like a real ball bounce but I want the ball to bounce like a ball in pong where ball don't fall down until it touches wall or platfotm but my ball goes in real life curve.
What I thing with curve is like when u kick a ball in the air and goes in curve but I want it to go unlimitly in flat line to the wall andbounce in a random direction.
Someone have any code ideas?
Make a physics material like the one below and place it on any surface that bounces. It is enough for your ball to have a Rigidbody with drag 0.
Also uncheck gravity to get the following result:
Basic Force Script:
After adding Rigidbody to the ball, you can use the following script for the initial force. Obviously you need more advanced algorithms for further development.
public Vector3 force = new Vector3(80, 40);
void Start() => GetComponent<Rigidbody>().AddForce(force);
Pong/breakout style physics is simpler than you might think.
The balls have no gravity applied to them, so all you need to do is apply a constant velocity to the ball.
When it collides with an object, you would flip the velocity on the axis it collided.
For example:
ball is travelling up at 60px/sec, and horizontally at 120px/sec, at 60fps.
You add (60/60)=1 pixels vertically every frame, and (120/60)=2 horizontally.
It hits a wall, so you flip the horizontal velocity to -120px/sec.
Now it's still going up, but in the opposite direction horizontally.
If it hits a ceiling, same thing but flip the vertical velocity component.
If you'd rather it go in a random direction:
calculate the magnitude of the ball's velocity sqrt(vx^2+vy^2)
find the angle the wall is facing
pick a random angle within 180 degrees of that angle
use trigonometry to get the x/y components of that angle, and multiply by the magnitude.
Don't subtract/add a constant value to the velocity every frame, because then you'd be applying gravity, which isn't what you're looking for.

Change default collider bounce direction in Unity

I'm creating a Breakout clone in Unity and want to change the direction of the ball based on if the ball hits the left side or the right side of the paddle, as in the original version of the game. What is the best way of accomplishing this? I tried adding angled colliders to my paddle, but that causes the ball to fall slightly through the paddle before hitting the collider and changing direction.
You should have access to the rigid body that you are colliding with, which will tell you the angle you want to bounce at. Then, you do a late modification of the trajectory ("late" as in after your physics are calculated and applied for velocity). Something like this should work:
Rigidbody2D.velocity = newDirection.normalized * Rigidbody2D.velocity.magnitude;

Unity 3D Character Stuck on wall-colliders

I am currently working on a 3D Unity game in which you control a block through a labyrinth made out of blocks and have to avoid spikes, moving enemys and other traps.
Here a picture how it looks at the moment ( you are the blue cube and have to avoid the moving pink ones ):
The problem now is, that when I move along a wall, the player gets stuck and stops moving until I move in the other direction again (every part of the wall is as big as the player because I am generating it from an image).
I already tried everything with Physics materials and friction but it does not get better :(.
The problem is that your BoxCollider of the player is probably getting stuck on the edges between two of the wall colliders. Consider "smoothing" the edges of the collider a bit so the player wont get stuck.
The default collision detection mode in Colliders is Descrete, it might jump through a small gap occasionally, you could set the players collider to CollisionDetectionMode.Continuous it will prevent the overshoot.
Try decreasing the Default Contact Offset in Edit/Project Settings/Physics
Changing it from 0.01 to 0.0001 worked for me
Source

Categories

Resources