- Home /
Problems with Physics(Flipping strangely)
There are a cube and floors in scene. I'm trying to move a cube on X axis with a script(I will need a jumping function on a key press later if i can solve this problem).
1. Cube has a Rigidbody which has 0 angular drag and 0 drag.
2. Both Cube and Floor have Box Colliders
3. Both Box Colliders have Physics Materials which has 0 friction.
4. Use Gravity on Rigidbody is active.
The problem is that the cube behaves really strange and flips and rotates even though I'm just setting a velocity on +X direction on a floor. I tried freezing constraints but then cube totally stops. Floors are 1 unit on X and Z axis since I am creating an endless path but they are next to each other seamlessly The scripts I have tried and didn't solve the problem :
void FixedUpdate()
{
Vector3 velocity = rb.velocity;
velocity.x = 5f;
rb.velocity = velocity;
}
void FixedUpdate()
{
rb.AddForce(5f-rb.velocity.x,0,0,ForceMode.VelocityChange);
}
Answer by Anis1808 · Dec 06, 2019 at 04:24 AM
I don't see errors in your code although I would simplify it to:
Vector3 vel = new Vector3(5,0,0);
rb.velocity = vel * speed;
This said, have you tried moving it with simple transform.position movement ? Does it work when you remove gravity ? Does your box have a very large mass ? It all depends on multiple variables but I suggest you play around with values.
I tried using position movement, it works better but if there is a wall, it tries to enter inside wall. Also I need a gravity for later. I tried many things after that but couldn't find any answer.
If you don't have a friction i really dont understand what is happening, can you post a picture of your scene because the one you posted didnt load.
Flips like this at start Also if I freeze rotation on Z, it won't move, just stucks at the first tile.
I have tried replicating your problem but I am not able, can you try a new clean scene and put a plane ins$$anonymous$$d of floors? If it works, can you use a cube from unity? $$anonymous$$aybe the collisions make it act this way
Answer by lgarczyn · Dec 06, 2019 at 07:09 PM
This is likely caused by very small imperfections on the floor colliders. Your cube basically gets stuck on the small ridges of the floor caused by the different columns.
You can
Make every flat surface is a single collider
use a CharacterControllerroller instead of a Rigidbody
Change the cube collider so that it has small bevels on the side and doesn't get stuck
Use 2D physics since the game appears to be mostly 2d gameplay with 3d graphics.
$$anonymous$$y path script doesn't work like that so I used 4th answer.
I couldn't find a good way to use it with jumping, need a tutorial.
?
It works pretty good for now. I will add some new mechanics and will see how it works. (Edit : my bad, it still flips :( )
Thanks anyway. I will try 2nd and 3rd answer soon.
2d physics won't help you too much with this exact problem, but will give your more options.
Simply generate a PolygonCollider2D for your player, then edit it to round the corners. Then just freeze the rotation.
Your answer
Follow this Question
Related Questions
Setting a RigidBody's velocity messes with my custom gravity, not sure how to proceed. 0 Answers
Any way to gracefully detach object with Unity physics? 2 Answers
Moving rigidbody (Player) with addForce or Velocity ? 1 Answer
Rigidbody.velocity seems to be breaking jumping physics 0 Answers
VR Sword Physics, how to add force or velocity to the sword for impact when it hits an object 1 Answer