- Home /
Shakey/Sticky collisions on right movement
Hey thanks for reading!
I am having trouble with a movement script I wrote for a very simple game I am making (just to test out Unity).
THe game consists of a movable rigidbody (which I will refer to as the player) and a bunch of normal blocks with simple Physics materials.
Basically movement works like this
if(right button is down){transform.x += .03}
I know this is pseudo code but I am trying to give you an idea without swamping you with code.
The problem is, when I move the character block to either the left or right, and it collides with a block, if I hold down either left or right the player sticks to the block and shakes back and forth.
I assume this problem is because Unity is registering a collision, pushing back the player, but since the person is still holding down the right button, they move forward again, which causes Unity to register a collision and on and on and on.
I am unsure how to fix this. And I might be wrong in my assumption.
Any help that you could give would be EXTREMELY beneficial!!!
Thank you so MUCH!
EDIT: Okay here is the code:
var time = 0; var lastTrig = 0;
function Update () {
//Body cannot rotate
rigidbody.freezeRotation = true;
transform.position.z = 0;
//Moves the player left
if(Input.GetKey("left")){
transform.position.x += -.02;
}
//Moves the player right
if(Input.GetKey("right")){
transform.position.x += .02;
}
//The player jumps
if(Input.GetKey("space") && lastTrig != 3){
rigidbody.velocity.y = 15;
time++;
lastTrig = 3;
}
//Debug.Log(time);
}
//Collision with rigidbody (Player)
function OnCollisionEnter(collision : Collision){
if(rigidbody.velocity.y <= .2){
lastTrig = 0;
}
}
I know you don't want to give loads of code, but without it it's very difficult to see what might be wrong.