- Home /
Question by
thebegginer · Jan 31, 2014 at 09:24 PM ·
jump
I need character only jump
I have a character with this script:
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
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.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
With this code, character is moving to left and right. I need that only jump.
How to make it?
Comment
Answer by JSierraAKAMC · Jan 31, 2014 at 10:01 PM
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
Simple as that. I just removed everything unrelated to jumping.
Your answer
Follow this Question
Related Questions
How to force to jump eternally object? 1 Answer
Switching between animations 1 Answer
How to make a Homing Attack like in Sonic the HedgeHog? 2 Answers
Adding a jump feature help? 2 Answers