Unity 3D object with Rigidbody sliding - unity3d

I have a cube with Rigidbody attached to it would slide slightly whenever I pressed the play button. If I leave it for awhile, it would slide to other side of the screen.
Anyone know how to solve this problem without using the "freeze position"? I don't know what I messed up in my project...
This is what the object looks like:
Note: I need to use the gravity.
Thank you!

Heh! The solution here is:
You had a rigidbody on the floor :)
You don't do that :) Never.
If the "floor" surface is flat, then,
it will not slide.
You've got something strange going on, such as
"floor" is NOT flat
a feature like "Wind" turned on
perhaps other objects invisible in the scene you have forgotten about are nudging it
PhysX does not have a "mind of it's own". There is some simple reason it is moving.
Let's say the "floor" is indeed on an angle, so it SHOULD move, but you WANT it to NOT move.
What you obviously do:
Just as in the real world, put something there to stop it moving.
A small invisible wall will do the trick. That's PhysX!

Usually rigidbody sliding happens when a lower rigidbody has lower mass than a higher rigidbody (forcing down the lower rigidbody). Typical problem with player having say mass 80 jumping on a cube with mass 1. In this case the collision is so violent that the cube will probably fly out (not only slide).
The situation is very similar to the real world. Try to stay on a box of milk if you have 120 kg (ok, ok, 80 :) ).
When you try to eliminate this behavior, you need either increase the mass of the lower object or decrease the mass of the higher one or set the lower rigidbody to kinematic.

The solutions above is not proper way of solving the problem. Unity has more features of physics than mass. If you get sliding on movement or because of other objects you should add proper drag value on your rigidbody. For example , lets say you have blocks spawning over top of other blocks and this creates horizontal sliding. In my case I add drag of 1 to the objects which has mass of ~ 1 kg. It depends on the scene and you should try different values on your case. Do not use bigger values and angular drag if it is not important.

Related

How to get rid of acceleration during bouncing ? (unity 2D)

I don't know if this is the correct therm, but I want to get rid of any kind of forces that could modify the speed of my object. Let me explain : the game is 2D top-down view and the character fires a bullet that bounces x amount of time, WITH CONSTANT SPEED. Everything works fine, but when the bullet bounces fast and many times, its speed increases.
Also, second problem: I have a rotating object and when the bullet hits it, its speed increases (which makes sense, but I would like to know if there is a way to delete this effect) .
I would like to know if there is a way to solve my issues.
You need to check the material of the object for your first question (https://docs.unity3d.com/Manual/class-PhysicMaterial.html) you need to set Bounciness to 1 so it will maintain dame speed. If you set it to 0 there will be no bounce and probably you have a value bigger than 1 and so it's increasing.
In the second case it's all about the forces involving. The object bouncing on the rotating one is adding a force to that one. If you want to keep the second one with same rotation you could rotate it with a transform.Rotate and remove a Rigidbody or set it to static

Unreal Engine 4 - Collider component sweep issue

I have a 2D environment where an object falls (a trap) and a box collider component is moved with a timeline to follow the movement with SetRelativeLocation.
Since the trap is near a wall if I leave the Sweep option unchecked in some cases the character will stuck mid air between the trap and the wall.
Sweep off
If I turn on Sweep this will not happen and the character is pushed to ground correctly but since the collider "slows down" for some frames it seems to be in the middle of the sprite.
Sweep on
Teleport option seems to have no effect on this behaviour.
Is there any way to have the sweep effect with the collider keeping his velocity and keeping the structure as it is with the collider moved by a timeline?
I feel like I'm missing something really simple but I'm losing my head on this.
Thanks!
Since it's 2D, have you tried blocking the input when your character collides? I would add a simple collider in front of your character and when it is overlapped, I'd limit the input in the direction your character goes, ie.:
Character collides right
MoveRight(float Scale): Clamp Scale to (-1.f, 0.f); AddMovementInput(...
Hope it helps!

Unity 2D Bounce Back moving object when colliding with another object

I have an object that, after receiving its respective input, it moves this way:
mov = new Vector3((Input.GetAxis("Horizontal") * vel), 0, 0);
transform.position += mov;
But, I want it to bounce back, once it collides with an object.
I´ve made the procedures already (OnCollisionEnter2D(Collsion2D col){bla bla...}), but I need help with what happens on the collision (bouncing back the object)....
I´ve tried giving the collided object a bouncing material, but it just slows it a bit, my guess is that because of the constant force given by the acceleration.
Greetings.
If you move the object with transform.position what you are doing is basically a "teleport" so it will ignore the bouncing material. If you want it to bounce you have to write the physics code to detect a collision and change the movement or you can do addforce to move the object and it will detect collisions and react automatically.
you are teleporting the object at the current time. instead you should use the Rigidbody.addForce this will add a force in the specified direction thus if you do the opposite direction will "bounce" of the object. Another option would be to create a physics material then not bother with the code.
You are not using materials, right?
See if the content of this post may help you, the OP is using a formula using Raycast and the answer guides him to use the Raycast with Layers Maks:
2D bouncing formula doesn't work properly
There is this one also with fixed angles (like Pong), but it uses material (with values: friction: 0, bounciness: 1):
https://gamedev.stackexchange.com/questions/70294/get-gameobject-to-bounce-of-colliders
But if nothing makes sense and you are going crazy and might want to start from zero, there is this official video tutorial on bouncing and sliding in 2D:
https://unity3d.com/learn/tutorials/modules/beginner/2d/sliding-bouncing-2d

Unity physics about collision and energy

I have been working on a Unity ping pong game using the Leap Motion. I use rigidbody.MovePosition() to move the paddle. However, when I hit the ball (which uses gravity), the paddle launches it too far. Even when I change the masses of both, it doesn't do anything.
What variable should I change to reduce this energy going into the ball?
From the following link.
"MovePosition will put your object at the target location, no matter what. It may push aside other objects in a realistic way, or may blast them out of the way, or may just pass right through them. But it will gladly move you through a solid wall or a mountain.
If you're using MovePosition on a rigidbody to add from where you currently are, it looks like AddForce. With AddForce, the physics step does all the work: applies your velocity, sees the collision and handles the bounce. With MovePosition, the physics step sees you're mysteriously overlapping a solid object. If it isn't too much, it will bounce you apart."
You won't need to use MovePosition. Instead, you can figure out the direction of the shot (based on the position of the ball relative to the paddle). Then you can add an impulse force in that direction to the ball.
Pseudo-code (from the following link):
Vector3 shootDir = ballPosition - paddlePosition; // Calculate direction of the shot
shootDir.Normalize(); // Normalize the shot vector
ball.AddForce(shootDir * speed, ForceMode.Impulse); //Add impulse force in correct direction.
Credit due to Owen Reynolds and Tim Michels.

Slope limit on player movement

I use Unity3D and I have a player with a rigidbody. I add force tto the body for moving the player. My player walks over a terrain but is able to walk up mountains that are to steep to climb. I want to limit the player so it cannot walk up a slope that is to steep.
I know there is a CharacterController component that has this functionality, but I have to use the rigidbody, so I want the same but on my rigidbody.
I can get the normal of the triangle I am standing on, and calculate its angle, but I cannot seem to make the player stop moving up the slope. Only make the player stop moving (which makes the player unmovable once it hits a angled slope)
Any ideas how to solve this problem?
It's difficult to answer without more details on how you're using the physics engine. How/Are you using friction? What angle are you applying the force? Is it always horizontal or at the angle of the floor? Does the player have a mass?
Anyway I can think of a few ways to solve this
Go the pure physics route. Using player mass, friction, force angle, gravity, etc. Get the physics to handle these situation for you. This may take a fair amount of time and programming.
Keep the rigid body but fake the forces. Scale the force you are applying to the body of the player with the angle of the triangle the player is on. You can either use trigonometry to work out what you should apply or your own mapping. By your own mapping I mean set an angle where 0 force is applied (say 45 degree) and do a linear(or non linear) scale on the force applied so on flat ground force is 1 and at 45 force is 0.
Don't use rigid bodies. There is a reason most games don't use rigid bodies to control characters. It's hard and complicated and most of the time not worth the time it would take. Of course I don't know the details of your project so if this isn't an option, fine.
Hope that gives you some things to think about.