Hi, I am trying to make the player jumps controllable by allowing the player determine how high the player jumps. Basically if the player holds the jump button longer the higher the player goes until it reaches the max speed.
Well, you are missing a lot of info for anyone to help you. No deceleration so idk what type you dealing with.
You can use my own code which is in Boo, very easy to transfer to unityscript. Keep in mind “single” means float. It is also assuming 2D physics which ‘w’ and ‘spacebar’ are defined as jump inputs.
import UnityEngine
[AddComponentMenu("Character/TwoDPlatformController")]
[RequireComponent( CharacterController )]
class TwoDPlatformController ( MonoBehaviour ):
//public test = TwoDPlatformControllerJumping() Access another class will also appear in inspector
public walkSpeed as single = 6.0F
public runSpeed as single = 10.0F
public jumpSpeed as single = 8.0F
public gravity as single = 20.0F
public doubleJumpEnabled as bool= true
public extraJump as int = 1
public autoRotate as bool = true
public maxRotationSpeed as single = 360
private moveDirection as Vector3 = Vector3.zero
private curNumJump as int = 0
private curSpeed as single = 0.0F
internal jumped as bool = false
private jumpingApexReached as bool = false
private controller as CharacterController
def Update():
PlayerMovement()
HandleEvents()
def PlayerMovement ():
controller = GetComponent(CharacterController)
// While character is grounded allow to jump and horizontal movement
// Player is restricted in jump movement. One way jump if double jump disabled
if controller.isGrounded:
curNumJump = 0
jumped = false
// Assign run speed when pressing left ctrl
if Input.GetKey(KeyCode.LeftControl):
curSpeed = runSpeed
// Assign default walk speed
else:
curSpeed = walkSpeed
// Apply vector direction, only focusing on X- axis
moveDirection = Vector3( Input.GetAxis('Horizontal') * curSpeed, 0, 0 )
// If player has jumped
// Allow player to single jump while in air
// Check if player has reached jump apex, used for animation states
if hasSingleJumped():
SingleJump()
hasReachedJumpingApex()
// Player is in the air
// Allow the player the option of extra jumping, already checks if extra jumping is enabled
// Check if player has reached jump apex, used for animation states
// TODO: Future implementation, allow user to shoot while in air
else:
if hasExtraJumped():
ExtraJump()
hasReachedJumpingApex()
ApplyGravityForce()
// Apply new vector direction to the character controller
controller.Move( moveDirection * Time.deltaTime )
def SingleJump ():
// Immediately present number of allowed mid air jumps when landed
// Allow player to jump
moveDirection.y = jumpSpeed
jumped = true
def ExtraJump ():
// Allow player the capability to jump multiple times in mid air
// Number of times is based on 'extraJump'
// Once limit is reach player must land before jumping again
// When jumping horizontal movement is unlocked for that jump then it will be locked again
if curNumJump < extraJump:
moveDirection = Vector3( Input.GetAxis('Horizontal') * walkSpeed, 0, 0 )
moveDirection.y = jumpSpeed
curNumJump += 1
def ApplyGravityForce ():
moveDirection.y -= ( gravity * Time.deltaTime )
def HandleEvents ():
// Events taken place are of action types: shooting, special attacks, interactable object, etc
pass
def hasSingleJumped ():
return ( Input.GetButtonDown( 'Jump' ) or Input.GetKeyDown( 'w' ) )
def hasExtraJumped ():
return ( Input.GetButtonDown( 'Jump' ) or Input.GetKeyDown( 'w' ) and doubleJumpEnabled )
def hasReachedJumpingApex ():
if jumped and controller.velocity.y <= 0.0:
jumpingApexReached = true
return jumpingApexReached
else:
if controller.isGrounded:
jumpingApexReached = false
return jumpingApexReached
def getSpeed ():
return moveDirection.x
You can remove AddComponentMenu part. Change [RequireComponent…] to @script RequireComponent(…)
This uses the CharacterControllr provide by Unity which is a way of control the player without the interruption of physics to provide unique movements.