• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Christoher Randin

Is it recommended to use Unity's Character Motor Script

0 posts in this topic

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

[AddComponentMenu("Character/TwoDPlatformController")]
[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():
		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

 

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
		animation.Stop()
		animation.Play(idleAnimation.name)
	
	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 :
			animation.CrossFade(runAnimation.name)
			
		//Play walk Animation if moving in left or rigt direction
		elif Mathf.Abs( controller.getSpeed() ) > 0.1:
			animation.CrossFade(walkAnimation.name)
			
		//Play idle animation if no movement on X- axis and has not jumped
		else:
			if not controller.jumped:
				animation.CrossFade(idleAnimation.name)

			
		// 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():
			animation.CrossFade(walkAnimation.name)

 

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0