PlayerController Code
Last updated
Last updated
Create Animations as described in prior section. Delete all except one of the auto-created animatedSprite gameObjects in the hierarchy panel.
Rename the Animated 2DSprite GameObject: Player
Create a Folder in Assets: Scripts
Create a C# Script, by 'right-clicking' to open the menu, from the Project/Assets/Scripts folder: PlayerController
Add the script, as a component, in the Inspector Panel of the Player gameObject.
Double-click the PlayerController.cs script in the Scripts folder, this script should open in Visual Studio
Paste code listed below into the PlayerController class file in Visual Studio.
Build Solution in Visual Studio to compile your script.
Create a Folder in Assets: PreFabs
Drag your Player GameObject from the Hierarchy Panel to the PreFab's folder in your assets panel.
Select the Player GameObject in the Hierarchy, in the Inspector Panel, it should be similar to the image below.
Goal: Configure device input events to correspond with Animator Component
Input Manager: horizontal-axis, key,
enums, logic,
goal: modify Animator HeroState parameter value
generate valid state-transition events
Animator Controller: Integer Parameter: HeroState
Edit in Animator Window, with Player GameObject selected in Hierarchy
Animator states: state-node
each state-node corresponds to Animation Clip (motion)
Event transitions: transition arrows:
each transition arrow corresponds to a valid state transition-event
HeroState Conditions:
HeroState == 0: idle state-transition
HeroState == 1: walk state-transition
HeroState == 2: jump state-transition
Animation Clip - additional configuration using Animation Widnow
Flip( ) - modify left/right sprite orientation by modifying Transform.Scale.X of Player sprite.
Next Versions of Player Controller to add:
Rigidbody - physics motion ex. gravity
Collider2D
Floor - empty gameObject with
Sorting Layers - determine render ordering of sprites, etc
Physics Layers: Determine physics interactions:
example: Custom Layer: Ground