# 3rd Person Controller moving rigid body

Hi! I need to push different rigid bodies by my standard 3rd Person Controller. What i can to do this?

Simple question, but unobvious solution (for me )

Go to Assets â†’ Import Package â†’ Scripts
Now select your 3rd Person Controller and go to Component â†’ Scripts â†’ Drag RigidBody.
That should do it.

Sorry =) it was for 1st Person Controller

and this script not working

first, you want a physics controlled character, not a math controlled one.

With that in mind, I wrote one a while back and posted it here:

Back when I wrote it, I tested all kinds of things, one of them was the ability to push other physics objects.

ultimately its need to write own script for controller, not any other simple ways
okey, thnx

Well, if you want something basic, and still want to use a character controller, I made this basic one that simulates a rigid body character, but still uses a character controller.

``````#pragma strict

@script RequireComponent(CharacterController);

var speed:float = 8;
var gravity:float = 1;
var jumpSpeed:float = 17;
var terminalVelocity:float = 60;

private var cc:CharacterController;
private var moveDir:Vector3 = Vector3(0,0,0);
private var dTime:float = 0;
private var grounded:boolean = true;
private var platform:Transform;
private var lastPlatform:Transform;
private var turn:float = 0;
var turnSpeed:float = 100;

function Start ()
{
cc = GetComponent(CharacterController);
//Gets the added character controller component.
}

function Update ()
{
dTime = Time.deltaTime;

//Movement is allowed in air, so it doesn't have to be in the grounded if statement.
var direction:float = Input.GetAxis("Horizontal");
if(direction != 0)
{
moveDir.x = Input.GetAxis("Horizontal") * speed;
if(direction < 0 )
{
turn += turnSpeed * Time.deltaTime;
if(turn > 180)
{
turn = 180;
}
transform.rotation = Quaternion.Euler(0,turn,0);
}
else if (direction > 0)
{
turn -= turnSpeed * Time.deltaTime;
if(turn < 0)
{
turn = 0;
}
transform.rotation = Quaternion.Euler(0,turn,0);
}
//Using "Horizontal" instead of specifying keys makes porting to other
//systems (ps3/xbox) much easier as you don't have to recode ANY of the
//controls, you simply have to set the input of "Horizontal" in the Input Manager.
}else{
moveDir.x = 0;
//Stops player from moving left or right when key/joystick is not being used.
}

if(grounded)
{
//We can only jump if we aren't in the air.
if(Input.GetAxis("Jump") != 0)
{
moveDir.y = jumpSpeed;
}else{
moveDir.y = -.01;
//This resets our fall speed so we accelerate when walking off of an edge
//instead of just instantly falling at full speed.
//We can't set it to 0 otherwise we switch between grounded and not grounded
//ever other frame.
}
}else{
moveDir.y -= gravity;
//makes character fall accelerate as the player falls further and further.
}

if(moveDir.y <= -terminalVelocity)
{
moveDir.y = -terminalVelocity;
//Sets the maximum fall speed.
}

//Platform will only be null if we don't collide beneath us.
if(platform != null)
{
//Calculations used to make player move on platforms when platforms move under them.

if(lastPlatform != null)
{
//We need to make certain that the last platform we stood on is new.
//Without this, we would jump around the level based on the last object's
//relationship to the new objects position. Not a fun thing when playing a game.
var tmpMove:Vector3 = lastPlatform.position - platform.position;
//tmpMove.y = 0;
transform.position -= tmpMove;
Debug.Log(tmpMove);
}
lastPlatform = platform;
}
platform = null;

cc.Move(moveDir*dTime);
transform.position.z = 0;
//moves player based on the framerate. This solves different game speeds
//for different computers. Makes for smoother game play when at low fps.

if(cc.collisionFlags  CollisionFlags.Below)
{
grounded = true;
}else{
grounded = false;
}

//sets if we are on the ground or not.
}

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.5)
{
platform = hit.transform;
return;
}

// Calculate push direction from move direction,
// we only push objects to the sides never up and down
// if you wanted up and down pushing, change 0 to hit.moveDirection.y
var pushDir : Vector3 = 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 and decrease speed to 25% of the normal walk speed.
body.velocity = (pushDir * speed)/4;
}
``````

The important part for this is the OnControllerColliderHit function. This was something I made to push objects at a slower rate then my speed. (In this case, a little girl pushing a box and made her move at 1/4th the speed of normal walking)

I donâ€™t have access to a computer with unity on it here at work, but I believe this is the right script. I had one that did this, but would only push things with positive valuesâ€¦ I donâ€™t remember if this is the fixed one or not.

EDIT: OH!!! And I think that my platform script part is messed up too. But, it should give you a good idea of how to do it. If anything, you only need the OnControllerColliderHit function added to your script, and a variable for speed.