Player GameObject v1
Version 1 of the PlayerController.cs Script
Last updated
Version 1 of the PlayerController.cs Script
Last updated
Follow directions in the Animator Controller, Animation Steps section to create an animated player and assume that all references to the Player below are using that Animated Player GameObject.
Then we need to attach a RigidBody2D component to our Player gameObject. The Unity Manual explains that we need to add a RigidBody2D component so that we can use the Unity Physics Engine to move our Player in the scene. We can update the player's RigidBody.velocity or add a force to the RigidBody2D each frame in response to the user key-presses, to cause the gameObject to move in a realistic way.
In the PlayerController code below, we write code to allow the user to move the player using various keyboard input. We're using the UnityEngine Input default mappings so we want to provide access to move the player along the horizontal and vertical axis. We need to use a RigidBody2D component because it allows us to use UnityEngine physics engine to create realistic character movement.
Unity RigidBody Documentation Rigidbodies enable your GameObjects to act under the control of physics. The Rigidbody can receive forces and torque to make your objects move in a realistic way. Rigidbody 2D, the difference that in 2D, objects can only move in the XY plane and can only rotate on an axis perpendicular to that plane. Remember to set gravity to 0 if you don't want gravity acting on your gameObject. 1. Add a Rigidbody2D to the Player gameObject. Expand the constraints option and check option to Freeze Rotation: Z as true.
Collider interactions Colliders interact with each other differently depending on how the Collider and Rigidbody components are configured. Collider Components define the shape of an object for the purposes of physical collisions. A collider, which is invisible, does not have to be the exact same shape as the object’s mesh. A GameObject that has a Collider but no RigidBody component will act like an immovable object that other objects can run into, the collider gives the gameObject a physical presence in the scene. When isTrigger
is selected, then the collider does not show physics-based collision behavior, instead, an event is generated that can be used to execute related actions or behaviors.
2. Add a Collider2D to the Player gameObject. Adjust the boundary to fit the player sprite. I have used a Capsule Collider2D, you can use other types of colliders to fit your sprite.
If we want to constrain the player, to keep her on the screen, we can create an empty game object and attach a collider2D component to that gameObject. We can use this to create a floor in our scene, this will allow us to have a non-zero value for gravity on our player's Rigidbody2D component. We can also create a leftBorder as an empty gameObject, again, add a BoxCollider2D component. If we don't set this gameObject to be a trigger, then it will act as a physical barrier to our playerGame object so it can't leave the camera view area.
3. Create an empty gameObject: name: Floor, attach a collider2D, add an icon by selecting the leftmost dropdown in the top section of the inspector.
Create a C# Script: PlayerController in the Scripts folder of your project's Assets. Double-Click on the script to have it open Visual Studio so you can edit the script. You can copy and paste the code from below into the script. In Visual Studio, build the script to compile the code so it can be used in Unity. Add the PlayerController.cs script as a component to the Player GameObject.
This script requires an Animator Component on the Player GameObject
In this version of the PlayerController script, we'll add logic for Horizontal movement using the Rigidbody2D component. We'll also add a method: Flip( ) that will use to reverse the sprite if necessary so that it faces the direction of movement. We'll add variables for forceX, the magnitude of horizontal force applied to the gameObject each frame that the player is in the walk state. In this version of the code, we'll zero-out the velocity