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.
Related
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;
I am a newbie to unity. In my project, the ball should instantiate in the player
position and it should go randomly 40 to 120 degrees from the player position.
I am using transition.forward. My player is constantly in running. when the player stands the ball is going correctly in front of the player.
but when the player runs the ball is going backward
How can I move towards randomly in front of the player? Like a ball
I am using the below code.
transform.Translate(Vector3.forward*5f );
There is a chance it might be fixed if you spawn the ball in front of the character. Since it works when the player is stationary, it might be conflicting with the collider of player when it is moving.
Also translating will just move the ball, not give it a velocity or anything. You should take a look at https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
So, first translate the ball forward and see if it spawned in the right location.
Then add a force vector to the ball's rigid body in the direction you like.
Something like:
ball.transform.Translate(Vector3.forward*5f);
float thrust = 10;
var rb = ball.GetComponent<Rigidbody>();
rb.AddForce(transform.forward * thrust);
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.
I'm making a maze whit Unity3D where a ball can roll and find the out way. Because the ball can be hide after a wall, I want to rotate the camera to a better position in function of the direction of the ball.
Take this example: the ball is rolling in to you (in the direction of the black arrow). So you can see or beter can't see, is where the ball is rolling to. So the camera must turn to the other side of the ball. If the ball rolls away from you must the camera turn to the original location.
The problem is now, I know how I can replace the camera but not in function of the direction? Can anyone help me with this? I'm just starting with Unity3D. Language behind I use C#.
Here is another situation where it is better to rotate the camera. (up: is scene, below game mode).
You can use this to set the camera position behind the ball based on the velocity and then the rotation in the direction of the ball
Vector3 offset = new Vector3(1,1,0);
transform.position = ball.transform.position - ball.GetComponent<Rigidbody>().velocity / ball.GetComponent<Rigidbody>().velocity.magnitude + offset;
transform.LookAt (ball.transform.position);
I have a screen filled with circles. One circle is the player, one is the enemy, and the others are obstacles. I want the enemy to be able to calculate if there is an obstacle in the path of a straight line from the player to the enemy, so it can adjust accordingly (it is taking place in space, so it is a straight line for jumping from asteroid to asteroid).
Right now, I just turn a random direction when the AI is stuck on an asteroid.
A simple solution could be find the Vector2 between player and enemy (using Vector2.Subtract), and then divide it into smaller vectors or reduce it to a delta that you choose. This can be done normalizing the distance vector and multipling it by a fixed number.
Then simply add that delta to the enemy position and check if the new position collides with an objects (maybe using Rectangle.Contains method), and do this until you reach the player.