- Home /
What is wrong with this movement script?
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)
Answer by aldonaletto · Jul 03, 2011 at 02:57 AM
Well, there's nothing wrong with the script, but due to some reason it's not working for you. I merged your older script and added a small velocity down the Y axis, then used Move instead of translate. It worked with a simple cylinder which had a Character Controller instead of a collider.
public var MoveSpeed: float = 15;
public var MoveLSpeed : float = 5;
function Update () {
var MoveFWD : float = Input.GetAxis("Vertical")* Time.deltaTime * MoveSpeed ;
var MoveLeft : float = Input.GetAxis("Horizontal")* Time.deltaTime * MoveLSpeed;
var MoveDown : float = Time.deltaTime * -10; // applys a small vert velocity
var controller = GetComponent(CharacterController);
controller.Move(Vector3(MoveLeft, MoveDown, MoveFWD));
}
@script RequireComponent (CharacterController)
Yep fantastic that solved it :0 cheers :) thank you so much
Your answer
Follow this Question
Related Questions
Why does my code works? 0 Answers
How to get bullets to hit crosshair 2 Answers
Is Unity Tutorials messing up my project? 1 Answer
How can I stop the fps controller jumping up slopes? 1 Answer
unlocking weapon (fps) 1 Answer