Ok that my code
var speed=10;
var rotateSpeed:int=50;
var gravity : float = 20.0;
function Update ()
{
if(controller.isGrounded)
{
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
var y = 0;
var z = Input.GetAxis("Vertical") * speed * Time.deltaTime;
transform.Translate(0 , y, z );
moveDirection.y -= gravity * Time.deltaTime;
}
}
But it doesn’t work what’s the problem?How can I make my player to jump with this code?
What do you mean by ‘it doesn’t work’? Do you mean jumping doesn’t work? If so, that’s because there’s no code there that would make the character jump.
(Also, you’ll probably want to use CharacterController.Move() or .SimpleMove() rather than Translate().)
You’re actually missing a good chunk of code in there, here’s the standard FPS script:
var speed = 6.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;
}
}
// 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)
Mainly just the “if (Input.GetButton (“Jump”))” section
Oh shit!I forgot this…but how can I make the player rotate?
No need for two topics, but anyways, this is what you need to do in order to change your FPS script into a third-person walker script
Rotate FPS/Third Person script:
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var rotateSpeed = 3.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(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
The only thing you need to do, is:
Add a value named rotateSpeed (and give it a value)
Edit the GetInput.Axis line to only have Vertical
and add in the transform.Rotate line
You will then have your character rotate instead of strafe sideways with this simple code, have fun!