Hi guys i created a script for my FPS player which allowed him to move left and right and up and down and etc. No the problem was my old script avoided objects and did not have any gravity added to it. So i posted a question and i was told if i use this it would work as well and give my player gravity and etc.
Now if i press the up arrows or the wsda keys the player will not move he will fly up in the air why is that ?
here is the new script :
/// This script moves the character controller forward
/// and sideways based on the arrow keys.
/// It also jumps when pressing space.
/// Make sure to attach a character controller to the same game object.
/// It is recommended that you make only one call to Move or SimpleMove per frame.
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection = Vector3.zero;
function Start(){
gravity = 20;
}
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetKey(KeyCode.Space)) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
i would be thank full if someone could help me
ps this was my old script which was working find but no gravity and collision. i am using a character controller by the way.
Old Script:
public var MoveSpeed: float = 15;
public var MoveLSpeed : float = 5;
function Update () {
if (Mathf.Abs(Input.GetAxis("Vertical")) > 0){
var MoveFWD : float = Input.GetAxis("Vertical")* Time.deltaTime * MoveSpeed ;
transform.Translate(Vector3.forward * MoveFWD);
}
if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0){
var MoveLeft : float = Input.GetAxis("Horizontal")* Time.deltaTime * MoveLSpeed;
transform.Translate(Vector3.left * MoveLSpeed);
}
}
@script RequireComponent (CharacterController)