Jump to content

View more

Image of the Day

#indiedev  #indiegame #screenshotsaturday https://t.co/IwVbswGrhe
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Is it recommended to use Unity's Character Motor Script

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 Cdrandin   Members   


Posted 07 January 2013 - 06:21 PM

I am in the process of creating the controls for my game which is a 3D modeled platform environment with 2D physics restrictions. I was using Unity's platform script and it didn't feel right as to how I wanted it.
So I am wondering if I need to put in the character motor script. So far I am just using the Character Controller component and adding in my own applied gravity, I will eventually add restrictions on movable platform object and more.

I understand the character motor a little bit, but I have only actually played with Unity for about a week and worked on the 3D platform tutorial, provided by Unity, getting a sense of the type of game I want to make and a 2D Mario copy using Unity, by WalkerBoysStudios.

So, far everything seems to be decently fine, my only problems i guess is with the animation part, but I have to flesh that out a bit more.

I was also wondering if I am using appropriate Unity functions for what I am trying to achieve or if there are better alternatives to what I am doing. I like to accept all criticism, but focus on my logic and implementation please.

This is my player controller script:

import UnityEngine

[RequireComponent( CharacterController )]

public class TwoDPlatformControllerJumping (  ):
	// Can the character jump?
	public enabled as bool = true;

	// How high do we jump when pressing jump and letting go immediately
	public baseHeight as single = 1.0;
	// We add extraHeight units (meters) on top when holding the button down longer while jumping
	public extraHeight as single = 4.1;
class TwoDPlatformController ( MonoBehaviour ): 
	//public 	test  = TwoDPlatformControllerJumping()  Access another class while 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
	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():
	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
				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():
		// 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
			if hasExtraJumped():

		// 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
	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
			if controller.isGrounded:
				jumpingApexReached = false
				return jumpingApexReached
	def getSpeed ():
		return moveDirection.x


and player animation controller script:

import UnityEngine

class CharacterAnimationController (MonoBehaviour): 

	public idleAnimation as AnimationClip;
	public walkAnimation as AnimationClip;
	public runAnimation as AnimationClip;
	public jumpPoseAnimation as AnimationClip;
	private jumpingApexReached as bool = false
	private jumpani            as bool = false
	def Start ():
		//Player will always start idle animation
	def Update ():
		controller = GetComponent(TwoDPlatformController)
		// TODO: Add character rotation when changing directions
		// Play run animation if player's velocity is greater than walk speed
		if Mathf.Abs( controller.getSpeed() ) > controller.walkSpeed :
		//Play walk Animation if moving in left or rigt direction
		elif Mathf.Abs( controller.getSpeed() ) > 0.1:
		//Play idle animation if no movement on X- axis and has not jumped
			if not controller.jumped:

		// If player has just jumped and has not yet reached it's apex animate jump animation
		if controller.hasSingleJumped() or controller.hasExtraJumped() and not controller.hasReachedJumpingApex():
			animation.CrossFade(jumpPoseAnimation.name, 0.1F)
		// If player is still in air and has reached apex, player falling animation, but for now set to walk animation
		if controller.jumped and controller.hasReachedJumpingApex():


I will find a place where I could upload the files if someone is interested in testing it out and helping me with a more hands on approach.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.