var speed = 6.0;
var runSpeed = 10.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
if(Input.GetButton("Shift")){
oldspeed = speed;
speed = runSpeed;
}
else{
speed = oldspeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
I applied it to a capsule and it doesn’t work. it doesn’t even apply the gravity…
Well, one thing I noticed is that it’s in FixedUpdate, not just Update… I don’t know if that has anything to do with it.
Try just Update for your function.
also, try transform.forward instead of transform.TransformDirection
EDIT:
So it’d be something more like this:
function Update() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = transform.forward;
moveDirection += new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection *= speed;
if (Input.GetButton ("Jump"))
{
moveDirection.y += jumpSpeed;
}
if(Input.GetButton("Shift"))
{
oldspeed = speed;
speed = runSpeed;
}else{
speed = oldspeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
}
I didn’t test this, so it may not work still. I’d suggest if you are just learning to look up TornadoTwins on youtube. I used them to learn when I started, and I feel fairly comfortable now. (I’m getting into more advanced stuff then I can handle most the time now, but it’s better to learn that way some times.)
Hmm…
I put that code in but now only the gravity works.