x


How can I make my player (a charactercontroller) push rigidbody objects?

How can i make my script driven player and ai character interact with rigidbodys. So the player can push away boxes.

more ▼

asked May 18 '10 at 01:01 PM

Mattivc gravatar image

Mattivc
1.7k 55 60 67

(comments are locked)
10|3000 characters needed characters left

7 answers: sort newest

The built-in CharacterController class has special status in the physics engine which means that it doesn't interact with other physics objects by default.

In order to be able to push objects around, you need to add a script, as described in the CharacterController class manual page. It gives this example:

// 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;
}
more ▼

answered May 18 '10 at 01:37 PM

duck gravatar image

duck ♦♦
41k 92 148 415

I'm also interested in this for future reference, how do you make it so they can only push certain objects, is that simply by naming a specific game object and if so what would that script look like... sorry, new to this whole scripting thing too. ;)

Feb 13 '11 at 01:05 AM Michael 12
(comments are locked)
10|3000 characters needed characters left

I use a modified version of the example. It's more useful if you want to interact via physics, I personally think my solution makes more sense. This solution will apply force to the point of contact. This means that if you bump into the corner, the object will rotate, if you hit the top, it will tip over. It's by no means absolutely perfect but it is better and still very simple.

The one also works with up and down so if you ever wanted to use a teeter totter you could. If you tried to simply enable downwards movement on the original script you won't get the intended results. I put this right into the CharacterMotor script which Unity provides, it really works nicely and makes things fun.

// this script pushes all rigidbodies that the character touches
var pushPower = 2.0;
var weight = 6.0;

function OnControllerColliderHit (hit : ControllerColliderHit)
{
    var body : Rigidbody = hit.collider.attachedRigidbody;
    var force : Vector3;

    // no rigidbody
    if (body == null || body.isKinematic) { return; }

    // We use gravity and weight to push things down, we use
    // our velocity and push power to push things other directions
    if (hit.moveDirection.y < -0.3) {
       force = Vector3 (0, -0.5, 0) * movement.gravity * weight;
    } else {
        force = hit.controller.velocity * pushPower;
    }

    // Apply the push
    body.AddForceAtPosition(force, hit.point);
}
more ▼

answered Aug 19 '12 at 07:50 PM

titegtnodI gravatar image

titegtnodI
3 1 2

Nice ! What is your variable movement ? "Unknown identifier: 'movement'."

Nov 12 '12 at 10:15 AM anthonyk2

I could be wrong but I believe it's in the default CharacterMotor script. If it's not use a number like "20" (my value) or "10" (the default) instead of "movement.Gravity".

Nov 12 '12 at 08:21 PM titegtnodI
(comments are locked)
10|3000 characters needed characters left

Wont rconise "moveChar"

Assets/trampoline test.js(24,29): BCE0005: Unknown identifier: 'moveChar'.

more ▼

answered Dec 18 '10 at 04:17 PM

cyberpwn.com gravatar image

cyberpwn.com
1

(comments are locked)
10|3000 characters needed characters left

Thanks for you help Duck, but the character should not climb on the cube. If we try to push the object.

more ▼

answered Jul 29 '10 at 03:40 AM

karthik gravatar image

karthik
96 7 7 10

Don't answer your own question with a comment.

Jul 29 '10 at 03:54 AM qJake
(comments are locked)
10|3000 characters needed characters left

I've recently come up with a funky solution to a similar problem: I had to go through a revolving door, pushing the blades of the door with my CharacterController. The revolving door was done with a (very very low poly) mesh collider and a RigidBody component held in place by a vertical hinge joint.

After I found out that a CharacterController isn't able to influence RigidBodies I created a new CapsuleCollider with the same size like the one my CharacterController used, attached a RigidBody component to it and wrote a very little script that always places this new CapsuleCollider at the same position that my CharacterController currently occupies. - That way the Capsule was pushing the doors like it would be pushed by the CharacterController itself. I then added a few lines of code to my script to switch off all physics on the CapsuleCollider at a certain distance from the door so that I could save the calculations for the physics when not needed.

That's probably not a good solution at all, but it worked perfectly and without any noticeable negative effect.

more ▼

answered Jul 29 '10 at 12:57 AM

Whimsical gravatar image

Whimsical
225 6 7 18

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2497
x1877
x1793
x1701

asked: May 18 '10 at 01:01 PM

Seen: 14035 times

Last Updated: Nov 12 '12 at 08:23 PM