Hey, i’m playing around when some movement for at character, i’m using a rigidbody, and i’m moving the charater around, only in the X and Y axis, on boxes with box colliders. I’ve build the world up from 1x1x1 boxes. When i move the character sideways, he sometimes bump / bounces / jumps a little bit into the air, i think its because he bumps into the corner of a box, even that the boxes are precise next to each other. If i make a long box, he moves smooth, but the idea is to make the world up from boxes. The movement script looks like this:
var distToGround: float;
function Start () {
// get the distance to ground
distToGround = collider.bounds.extents.y;
}
function IsGroundedLeftSide(): boolean {
return Physics.Raycast(transform.position + Vector3.left*0.3 , -Vector3.up, distToGround );
}
function IsGroundedRightSide(): boolean {
return Physics.Raycast(transform.position + Vector3.right*0.3 , -Vector3.up, distToGround );
}
private var moveForce : float = 4;
private var jumpForce : float = 5;
private var gravity : float = 10;
private var moveDirection : Vector3 = Vector3.zero;
function FixedUpdate () {
//Constant get move direction
moveDirection = Vector3 ( Input.GetAxis ("Horizontal"), 0, 0 );
moveDirection = transform.TransformDirection( moveDirection );
moveDirection *= moveForce;
var velocityChange = (moveDirection.x - rigidbody.velocity.x);
//Movement in X-axis
//rigidbody.AddForce (velocityChange,0,0 , ForceMode.VelocityChange);
rigidbody.velocity.x = moveDirection.x;
// Gravity //
rigidbody.velocity.y -= gravity * Time.deltaTime;
}
function Update () {
// ----- Check for touching the ground ----- //
var down = transform.TransformDirection (Vector3.down);
if ( IsGroundedLeftSide() || IsGroundedRightSide() ) {
//print ("I touch ground!");
gameObject.renderer.material.color = Color.blue;
if (Input.GetButtonDown("Jump")) {
//rigidbody.AddForce ( 0, jumpForce * rigidbody.mass, 0 );
rigidbody.velocity.y = jumpForce;
}
}
// ----- When NOT touching the ground ----- //
if ( !IsGroundedLeftSide() !IsGroundedRightSide() ) {
//print ("I DO NOT touch the ground !!");
gameObject.renderer.material.color = Color.red;
}
}
I should mention the character is frozen in postion Z and roation X/Y/Z and gravity is controlled by script.
Also, when he lands from a jump, he sometimes bounces back into the air just a little bit, but i want him he stay in his position in the Y axis when i touches ground.
Somebody have an idea/solution? //If you dont understand the question/problem, ask, and i will try to explain myself better.
Thanks, Rasmus
Every modification such as this needs be time based rather than frame based (since frames aren’t uniform). All of your velocity modifications need to be multiplied by Time.deltaTime.
Okay, i multiplied the velocity modifications with Time.deltaTime, only resulting in i had to increase the moveForce significantly, but he still bounces when i move him ?
Tried everything, looked everywhere…
I just want a simple physics based movement for at 2D character. I have a box that is frozen in rotation: X,Y,Z and in position Z. It has a rigidbody attached. I move it with rigidbody.velocity = “something”… When i move it over a series of boxes, it collides with the edges, and bounce up a bit, resulting in character not being grounded and unable to jump. What to do ?
Alternatives:
use transform.Translate for movement = Very bad collision
use character controller = cannot change shape from capsule to box, so character has a round bottom, resulting in ugly movement.
What to do =?
This is an issue with ghost vertices. You can’t build your level tile based using cube colliders on a character that is also a cube. It causes intersection check errors when crossing over the parts that meet, due to one thinking pushing you upwards is the quickest way out of the collider, while the one in front thinks pushing you backwards is the quickest.
To avoid this, either use polygon/planes as colliders for your surface, or switch to the new 2D system and use EdgeCollider 2D’s to make up your surface (this is preferable, since you can then easily define each side as a different layer, for various collision detections)
Thx, i solved the problem with having one huge block with a collider, but still thats a lot of extra work. 2D rigidbodies falls trough both edgeCollider and polyCollider… And having single polygons instead of cubes dosent solve the problem
Okay, in case anyone anyone should stumble upon this very same issue, it can be solved by Edit → Project Settings → Physics → Min penetration for penalty and set it to zero. Now i can move my rigidbody character around by manipulating it’s velocity, and use unitys 1x1x1 cubes as tiles for a 2D/3D game
nice one, this was bothering me the last coulple of days, tried edge collider at the feet and alls sorts to try and get a smooth movement using velocity.
this works a treat so far, now i can use my tiled platforms together and build a scene as I go with just the prefabs.
trying to redo manic miner to learn a bit about 2D stuff
I eventually ended up using raycasting instead of the collider for the player for that same reasons, bit more to learn, but for me it worked.
heres a couple of pages that i got info from.
FIXED: - Go to Edit > Project Settings > Physics and raise “Default Solver Iterations” and
“Default Solver Velocity Iterations” - had to raise mine to 12 and 24 - now collisions have never been so smooooth.
this doesn’t work for me. I’m using a tilemap collider 2d and it still bounces when moving even after changing what you said. my tilemap colliders is using 0.1 as it’s default scale though. Is this the problem???
I had this problem also in Unity 2020.3.18f1, and I noticed that I had to change two settings for my dragon with a capsule collider running around at 20m/s.
Set Default Contact Offset to 0.0001 and make sure the rigidbody has the collision mode set to ContinuousDynamic.
Changing the Default Contact Offset didn’t make much difference if I had the rigidbody mode set to ContinuousSpeculative.