- Home /
Question by
uchiu · May 04, 2014 at 10:51 PM ·
character controllerfog
The Player doesn't go forward
Hi guys, I have this script but the character go only left, right and backwards... but not forward, why? In the end... the player doesn't jump :(...
#pragma strict
public var Player : GameObject; //Player GameObject
public var Speed : float = 6.0; //Speed
public var JumpSpeed : float = 8.0; //Jump Speed
public var Gravity : float = 20.0; //Gravity
public var JumpSound : AudioClip; //Jump Sound
var Health : int = 100; //Health
var HealthHUD : GUIStyle; //Health GUI
private var moveDirection : Vector3 = Vector3.zero; //Move Direction
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= Speed;
if(Health == 0)
{
Destroy(Player, 0); //Destroy player when health = 0
}
if (Input.GetButton ("Jump")) { //If the jump key (Space Bar)
moveDirection.y = JumpSpeed; //Then lift the player in the direction "y."
audio.PlayOneShot(JumpSound); //Then play the audio clip in "Jump Sound"
}
}
// Apply gravity
moveDirection.y -= Gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
Comment
The not being able to go forward is usually due to a child game object on your character like a weapon or a hand. If you have a child object, start by turning off he collider on the child.
Your answer
Follow this Question
Related Questions
Attaching a chararter controller to a Rigidbody car? 0 Answers
How can I use a joystick for a mobile app to control specific limbs of a character? 0 Answers
How To Improve my Rigid Body Sphere Character Controller 2 Answers
2D platformer. What is the best option for the main Character? 1 Answer
Character controller is moving left diagonally even if I don't press anything. 1 Answer