- Home /
Newbie: How to "push" a cube using mode FPS
Hello, first of all im really really new...
I download the 2DGamePlayTutorial and im notice that the "boxes" or crates can be pushed if the characters collision with them.
Now i make my own "game" or just im using a FPScontroller, i create a cube with collider and rigidbody, but when i collision with the cube does not move is like solid and attached to the ground (my ground or floor is another cube). I want to walk and when i collision the cube, this can be pushed and fall...
I try to import the crate from the 2DGamePlayTutorial and it does not work
Any suggest?? thanks in advance
Erick
Answer by erickrmz · Feb 23, 2012 at 03:52 AM
I did it myself:
Create a Javascript file on Project window i called "Push.js"
COpy the code from script documentation, this is the script:
// this script pushes all rigidbodies that the character touches var pushPower = 2.0;
function OnControllerColliderHit (hit : ControllerColliderHit) { var body : Rigidbody = hit.collider.attachedRigidbody;
// no rigidbody if (body == null || body.isKinematic) { return; } // We dont want to push objects below us if (hit.moveDirection.y < -0.3) { return; } // Calculate push direction from move direction, // we only push objects to the sides never up and down var pushDir = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z); // If you know how fast your character is trying to move, // then you can also multiply the push velocity by that. // Apply the push body.velocity = pushDir * pushPower; }
I drag the file into "First Person Controller" on "Hierarchy window"
And thats it, i can push all objects with Rigidbody :)
Your answer
Follow this Question
Related Questions
Making Colliders/Triggers or Rigidbodies move a Character Controller 5 Answers
How to destroy the cube which I selected with the arrow key 2 Answers
Why is my object getting stuck in the edge of other objects? 2 Answers
Cube with colliders ignores other colliders, why? 1 Answer
How to prevent a rigidbody from pushing other rigidbodies? 1 Answer