- Home /
Joystick - movement and jump in the same direction
Hi, I have looked in to Penelope's character controller scripts and played around with the code to come up with the code below. At the moment the left joystick moves the player and faces the direction you leave the character, even on release of the joystick (which is what i want). However, I am struggling to make the player jump in the direction the player is facing. The right joystick plays the jump animation how I want it, but it only jumps forward. Any suggestions? I really cant work it out!
var rotationPlayer : Quaternion;
function Start() {
thisTransform = GetComponent( Transform );
character = GetComponent( CharacterController );
animationController = GetComponent( AnimationController );
animationController.maxForwardSpeed = speed;
var spawn = GameObject.Find( "PlayerSpawn" );
if ( spawn )
thisTransform.position = spawn.transform.position;
}
function FaceMovementDirection() {
horizontalVelocity = character.velocity; horizontalVelocity.y = 0; // if ( horizontalVelocity.magnitude > 0.1) //botTransform.forward = horizontalVelocity.normalized * 180; }
function OnEndGame() { // Disable joystick when the game ends
moveJoystick.Disable(); rotateJoystick.Disable();
// Don't allow any more control changes when the game ends
this.enabled = false;
}
function Update() { rotationPlayer = transform.rotation; var movement = cameraTransform.TransformDirection( Vector3( moveJoystick.position.x, 0, moveJoystick.position.y ) ); // We only want the camera-space horizontal direction
movement.y = 0;
movement.Normalize(); // Adjust magnitude after ignoring vertical movement
inAirMultiplier = velocity.z;
// Let's use the largest component of the joystick position for the speed.
var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) );
movement *= speed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
// Check for jump
if ( character.isGrounded )
{
if ( rotateJoystick.tapCount == 2 )
{
velocity.y += 16;//height of jump
Debug.Log(velocity.z+" velocity");
}
}else{
velocity.y -= 1;
if(velocity.z < 1){
movement.z += rotationPlayer.z + 20;
movement.x += rotationPlayer.x;
}
else{
movement.z += velocity.z + 0.25;
}
}
Debug.Log(horizontalVelocity);
movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;
character.Move( movement );
if ( character.isGrounded )
velocity = Vector3.zero;
FaceMovementDirection();
var camRotation = rotateJoystick.position;
camRotation.x *= rotationSpeed.x;
camRotation.y *= rotationSpeed.y;
camRotation *= Time.deltaTime;
}