- Home /
2 cubes objects bounce off eachother
Hello, I have made a 3D versjon of pong.
I have a a paddle that player1 controls with this script. It works fine all directions as it should.
It is a cube object with rigidBody. It has a pysical material on it and on all walls (plane with mesh colider) so the ball can bounce of it.
PysMat: Friction = 0 and Bounciness = 1.
The problem I am trying to solve is when my cube(paddle) hits any wall it starts to "shake and bounce" making it uncontrollable (looks like it gains force in some direction, if I let go off the controls it starts to move on its own). I have at least locked it from spinning of x,y axis by checking all 3 freeze rotation boxes.
I have the same problem without the pysical materials, it just shakes alittle differently. Is there a way I can make 'em not hit each other or constrain the movements of my player controlled cube?
my script for controlling the paddle (cube)
var keyboardSpeed = 20.0;
function FixedUpdate () {
var keyboardX = Input.GetAxis("Horizontal") * keyboardSpeed * Time.deltaTime;
var keyboardY = Input.GetAxis("Vertical") * keyboardSpeed * Time.deltaTime;
var newPos = rigidbody.position + Vector3(keyboardX, keyboardY, 0.0);
rigidbody.MovePosition(newPos);
}
@script RequireComponent(Rigidbody)
Answer by ScroodgeM · Jul 30, 2012 at 09:33 PM
solution 1: you should mark rigidbody as kinematic if you want control it directly
solution 2: use 'add force' instead of 'move position' if you want to control it and let physics work
edit: fast example of what i mean
http://zammyart.com/UA/RigidbodyTester/WebPlayer.html
arrows or WASD - give ball a force
void Update() { rigidbody.AddForce(50f * new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"))); }
sources
http://zammyart.com/UA/RigidbodyTester/rigidbodytest.unitypackage
I do not think solution 1 will work here without rewriting my code, but I will try solution 2 tomorrow, thanks.
I have tried solution 1 is kinematic, the the paddle just goes through the wall.
Solution 2 just made the steering harder and still bounces on the wall.
I have tried transform.position.x += transform.forward moveSpeed Time.deltaTime; ins$$anonymous$$d. Still same problem.
By adding drag i got the paddle to not drift of after hitting a wall.
Found that it was interpolate settings that made all the difference. By setting interpolate to none then the problem is livable combined with high drag settings.
you should generally select one of two ways:
you control object manually. it means rigidbody is marked as kinematic, you calculate all movements, speeds, collisions etc in scripts, and apply it directly to object
you don't control object manually. it means rigidbody is not marked as kinematic, unity calculates all physics interaction, and you able to change only forces that affects on object. like a car in reallife - you just can change force and rotate wheels.
so again. solution 2.
so if you will let physics to be calculated automatically and affect object with realistic forces, you'll get a realistic physics.
try to apply low forces to object. it can bounces cause mass is low and forces are high
Can you point me in the right direction for making my object not go through the walls when they hit if I use is kinematic? I needs to stop right beside the walls.
But I use physics when the ball hits the paddle. So the object should not be kinematic at that point I think.
Answer by Dutti · Jul 31, 2012 at 10:27 AM
I needed to have my paddle rigidBody Interpolate settings set at none and drag at something high like 100 to make the problem less noticeable.
Your answer
Follow this Question
Related Questions
How to get Satisfying Collision Physics 1 Answer
Character with box collider sticks to walls 0 Answers
How to make Rigidbody.AddForce less delayed in Unity3D? 0 Answers
Avoid bouncing when ground move down 1 Answer
Tracked Ride Physics Simulation 0 Answers