- Home /
tilt control for jump
Hi everyone please help me. I want my character to jump when tilting up and to move forward vise versa of down, but i got problem with my code, the code actually jump the player automatically when played even i don't tilt it up or down.
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...
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
Tilt control for jump 0 Answers
How to make camera position relative to a specific target. 1 Answer
Help with iphone/android touch controls 2 Answers
Disable movement control during jump 3 Answers
Getting Started on Android 1 Answer