- Home /
player collite with object and start moonwalking
trouble with collision with an object when the player collite with and object the player start to walk like moonwalk from the opposite direction if i do a debug.log to position the x positio or the y position start to increas o decrease an this look like moving by himself.
player like rigidbody and a box collider , the object a cube has rigidbody and a box collider this is the code i have the game is a 2d with movement from left to right also up and down
void FixedUpdate (){
float h = Input.GetAxis ("Horizontal"); //for A D input
float v = Input.GetAxis ("Vertical"); //for W S input
hMove = h * xSpeed;
//When input Horizontal is true, multiply h by moveSpeed
vMove = v * ySpeed;
//as above, but referenced to Vertical
zMove = v * zSpeed;
//as above, but for Depth
//... Free the speed from framerate's tiranny...
hMove *= Time.deltaTime;
vMove *= Time.deltaTime;
zMove *= Time.deltaTime;
//This line tells the game what to do with the lines above, that is to Move.
if (collite == false) {
Debug.Log (collite);
transform.Translate (hMove, 0.0f, zMove, Space.World);
}
//... And animate, referencing to absolute values since negatives would be wrong!
if ((Mathf.Abs (Input.GetAxisRaw ("Horizontal")) != 0) || (Mathf.Abs (Input.GetAxis ("Vertical")) != 0)) {
anim.SetFloat ("Speed", Mathf.Abs (1));
}
else {
anim.SetFloat ("Speed", Mathf.Abs (0));
}
if((facingRight && h < 0) || (!facingRight && h > 0)){
//this is the other way around
Flip ();
}
}
void OnCollisionEnter(Collision other){
if (other.gameObject.name == "Wall") {
Debug.Log(other.gameObject.name);
rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0);
//rigidbody.AddForce ((Vector3.left * 10.0f));
}
}
thanks for the help
ok finally found the answer looking youtube video and some code only for those that need help in this the trouble was i was using translate and when use translate the rigidbody is hard to control so i cahnge the movement with velocity and force and rigth now everything is O$$anonymous$$ so that was the answer jajajaja
Answer by omarsolarte · Apr 24, 2015 at 06:11 PM
ok finally found the answer looking youtube video and some code only for those that need help in this the trouble was i was using translate and when use translate the rigidbody is hard to control so i cahnge the movement with velocity and force and rigth now everything is OK so that was the answer jajajaja