- Home /
Unity Standard Assets Mobile First Person Controls not jumping
//////////////////////////////////////////////////////////////
// FirstPersonControl.js
//
// FirstPersonControl creates a control scheme where the camera
// location and controls directly map to being in the first person.
// The left pad is used to move the character, and the
// right pad is used to rotate the character. A quick double-tap
// on the right joystick will make the character jump.
//
// If no right pad is assigned, then tilt is used for rotation
// you double tap the left pad to jump
//////////////////////////////////////////////////////////////
#pragma strict
@script RequireComponent( CharacterController )
// This script must be attached to a GameObject that has a CharacterController
var moveTouchPad : Joystick;
var rotateTouchPad : Joystick; // If unassigned, tilt is used
var cameraPivot : Transform; // The transform used for camera rotation
var forwardSpeed : float = 4;
var backwardSpeed : float = 1;
var sidestepSpeed : float = 1;
var jumpSpeed : float = 8;
var inAirMultiplier : float = 0.25; // Limiter for ground speed while jumping
var rotationSpeed : Vector2 = Vector2( 200, 200 ); // Camera rotation speed for each axis
var tiltPositiveYAxis = 0.6;
var tiltNegativeYAxis = 0.4;
var tiltXAxisMinimum = 0.1;
private var thisTransform : Transform;
private var character : CharacterController;
private var cameraVelocity : Vector3;
private var velocity : Vector3; // Used for continuing momentum while in air
private var canJump = true;
function Start()
{
// Cache component lookup at startup instead of doing this every frame
thisTransform = GetComponent( Transform );
character = GetComponent( CharacterController );
// Move the character to the correct start position in the level, if one exists
var spawn = GameObject.Find( "PlayerSpawn" );
if ( spawn )
thisTransform.position = spawn.transform.position;
}
function OnEndGame()
{
// Disable joystick when the game ends
moveTouchPad.Disable();
if ( rotateTouchPad )
rotateTouchPad.Disable();
// Don't allow any more control changes when the game ends
this.enabled = false;
}
function Update()
{
var movement = thisTransform.TransformDirection( Vector3( moveTouchPad.position.x, 0, moveTouchPad.position.y ) );
// We only want horizontal movement
movement.y = 0;
movement.Normalize();
// Apply movement from move joystick
var absJoyPos = Vector2( Mathf.Abs( moveTouchPad.position.x ), Mathf.Abs( moveTouchPad.position.y ) );
if ( absJoyPos.y > absJoyPos.x )
{
if ( moveTouchPad.position.y > 0 )
movement *= forwardSpeed * absJoyPos.y;
else
movement *= backwardSpeed * absJoyPos.y;
}
else
movement *= sidestepSpeed * absJoyPos.x;
// Check for jump
if ( character.isGrounded )
{
var jump = false;
var touchPad : Joystick;
if ( rotateTouchPad )
touchPad = rotateTouchPad;
else
touchPad = moveTouchPad;
if ( !touchPad.IsFingerDown() )
canJump = true;
if ( canJump && Input.GetMouseButtonDown(0))
{
jump = true;
canJump = false;
}
if ( jump )
{
// Apply the current movement to launch velocity
velocity = character.velocity;
velocity.y = jumpSpeed;
}
}
else
{
// Apply gravity to our velocity to diminish it over time
velocity.y += Physics.gravity.y * Time.deltaTime;
// Adjust additional movement while in-air
movement.x *= inAirMultiplier;
movement.z *= inAirMultiplier;
}
movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;
// Actually move the character
character.Move( movement );
if ( character.isGrounded )
// Remove any persistent velocity after landing
velocity = Vector3.zero;
// Apply rotation from rotation joystick
if ( character.isGrounded )
{
if(Input.GetMouseButtonDown(0))
{
return;
}
var camRotation = Vector2.zero;
if ( rotateTouchPad )
camRotation = rotateTouchPad.position;
else
{
// Use tilt instead
// print( iPhoneInput.acceleration );
var acceleration = Input.acceleration;
var absTiltX = Mathf.Abs( acceleration.x );
if ( acceleration.z < 0 && acceleration.x < 0 )
{
if ( absTiltX >= tiltPositiveYAxis )
camRotation.y = (absTiltX - tiltPositiveYAxis) / (1 - tiltPositiveYAxis);
else if ( absTiltX <= tiltNegativeYAxis )
camRotation.y = -( tiltNegativeYAxis - absTiltX) / tiltNegativeYAxis;
}
if ( Mathf.Abs( acceleration.y ) >= tiltXAxisMinimum )
camRotation.x = -(acceleration.y - tiltXAxisMinimum) / (1 - tiltXAxisMinimum);
}
camRotation.x *= rotationSpeed.x;
camRotation.y *= rotationSpeed.y;
camRotation *= Time.deltaTime;
// Rotate the character around world-y using x-axis of joystick
thisTransform.Rotate( 0, camRotation.x, 0, Space.World );
// Rotate only the camera with y-axis input
cameraPivot.Rotate( -camRotation.y, 0, 0 );
}
}
So I'm using the Standard Assets (mobile) to create a Windows Phone 8 game. Specifically, the FirstPersonControl Script. My problem is with the jumping. I cannot jump. I have double-tapped the touchpad on my phone, and also set it to MouseButtonDown(0) instead. That didn't even work. I am using the Dual Joysticks. Now, if anyone could edit this script so the jumping would work, that would be amazing.
"Yet another example of no one answering my questions." - $$anonymous$$aybe you should consider whether that's something to do with the way you ask them...?
Anyway, does your Windows Phone 8 have a mouse? No. So why on earth would you rewrite the code to use $$anonymous$$ouseButtonDown(0)
? The standard script works fine for me - were you double tapping on the right touchpad?
$$anonymous$$ouseButtonDown(0) is one touch on the screen. I was spam$$anonymous$$g both touchpads.
That's undocumented behaviour and shouldn't be relied upon. If you want to react to a single touch tap, you should use (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began)
$$anonymous$$any scripts do use this behaviour, and just because it is undocumented doesn't mean I cannot use it.
Answer by eshonbel · Jun 18, 2014 at 07:19 AM
Solved - I just added the Character Motor script and fiddled around with it to create an awesome combination of Standard Assets Mobile and Character Controllers.