- Home /
Tilt control for jump
hi every one i need help for our project game. i need my character to jump when using tilt up and down but my code was causing to automatically jump the character when played.
here is my code
pragma strict
var speed : float = 6.0; var jumpSpeed : float = 8.0; var gravity : float = 10.0; private var vertVel: float = 0; // vertical velocity var moveDirection : Vector3 = Vector3.zero;
function Update () {
// we assume that device is held parallel to the ground
// and Home button is in the right hand
// remap device acceleration axis to game coordinates:
// 1) XY plane of the device is mapped onto XZ plane
// 2) rotated 90 degrees around Y axis
moveDirection.x = -Input.acceleration.y;
moveDirection.z = Input.acceleration.x;
// clamp acceleration vector to unit sphere
if (moveDirection.sqrMagnitude > 1)
moveDirection.Normalize();
moveDirection *= Time.deltaTime;
// Move object
transform.Translate (moveDirection * speed);
var controller : CharacterController = GetComponent(CharacterController);
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection) * speed;
if (Input.acceleration.z != 0) { // and Jump is pressed... // vertical velocity = jumpSpeed
if (controller.isGrounded) { // if it's grounded... vertVel = jumpSpeed;
}
} vertVel -= gravity Time.deltaTime; // apply gravity to the vertical velocity moveDirection.y = vertVel; // combine move direction with vertical velocity controller.Move(moveDirection Time.deltaTime); // and Move moveDirection = Vector3.zero;
}
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
tilt control for jump 0 Answers
Player Jump on Mobile phone? 0 Answers