PlayerController Code

Create a Prefab Player GameObject

  1. Create Animations as described in prior section. Delete all except one of the auto-created animatedSprite gameObjects in the hierarchy panel.

  2. Rename the Animated 2DSprite GameObject: Player

  3. Create a Folder in Assets: Scripts

  4. Create a C# Script, by 'right-clicking' to open the menu, from the Project/Assets/Scripts folder: PlayerController

  5. Add the script, as a component, in the Inspector Panel of the Player gameObject.

  6. Double-click the PlayerController.cs script in the Scripts folder, this script should open in Visual Studio

  7. Paste code listed below into the PlayerController class file in Visual Studio.

  8. Build Solution in Visual Studio to compile your script.

  9. Create a Folder in Assets: PreFabs

  10. Drag your Player GameObject from the Hierarchy Panel to the PreFab's folder in your assets panel.

  11. Select the Player GameObject in the Hierarchy, in the Inspector Panel, it should be similar to the image below.

PlayerController.cs Script - Sept 9, 2019

PlayerController version1 Summary:

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

Last updated

Was this helpful?