|
I'm kinda new to Unity. I'm a programmer and I want to make a capsule move, jump, shoot etc for a 2.5d sidescroller. Q1. Does the character controller use a Rigidbody for collision? If not, how do I have physics interaction with the world: like move crates around, push a ball etc? Q2. Does the character controller force capsule collider or can we customize it? Q3. I found this code in the docs: http://unity3d.com/support/documentation/ScriptReference/CharacterController.Move.html If I use it for a sidescroller, the character can't move while in air and the motion is unrealistic, any solution for this? Also, can you help me out with multiple jumps in the air without touching the ground.
(comments are locked)
|
|
1- No, the CharacterController doesn't use a Rigidbody (and attaching a rigidbody to it usually produce very weird results). You can use OnControllerColliderHit to push rigidbodies (see the example in the docs). To carry an object, you can child it to the character - but don't let it be in touch with the capsule collider, because it can disturb the character movement (it's a CharacterController bug).
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
private var vSpeed : float = 0; // keep vertical speed in a separate variable
private var controller: CharacterController; // controller reference
function Update() {
if (!controller) controller = GetComponent(CharacterController);
moveDirection = transform.right * Input.GetAxis("Vertical") * speed;
if (controller.isGrounded) {
vSpeed = 0; // a grounded character has zero vert speed unless...
if (Input.GetButton ("Jump")) { // unless Jump is pressed!
vSpeed = jumpSpeed;
}
}
// Apply gravity
vSpeed -= gravity * Time.deltaTime;
moveDirection.y = vSpeed; // include vertical speed
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
This code only allows the character to jump when grounded; if you want it to jump even when in the air, move the jump code outside the isGrounded if That's exactly what I was looking for, thanks for the help.
Dec 23 '11 at 05:30 PM
jorjdboss
Thanks! I was having trouble with this :-D
Mar 31 at 10:57 PM
dogzerx2
while hitting on another colliders, charcter controller moves bit up because of physics property. How to escape from this issue?
Apr 09 at 04:23 AM
sona.viswam
(comments are locked)
|
