Moving the player in a game is one of the most crucial aspects of game development.
In Unity 3D, the default player controller includes basic movement controls such as walking forward, turning left and right, and jumping. However, if you want to create more complex and engaging games, you may need to customize the player’s movement.
Custom Player Controllers with Scripting
Creating a Custom Player Controller Script
If you want to create a custom player controller, the first step is to write a script that defines the movement rules. You can create a new script in Unity by right-clicking on the Hierarchy and selecting "Create" > "C Script." Once you have created the script, you can add code to define the movement rules.
Here’s an example of a simple player controller script that allows the player to move forward, turn left and right, and jump:
csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed 5f; // movement speed of the player
public float turnSpeed 100f; // turn speed of the player
public float jumpForce 10f; // force applied when jumping
private Rigidbody2D rb; // reference to the player’s rigidbody component
private Animator anim; // reference to the player’s animator component
void Start()
{
rb GetComponent();
anim GetComponent();
}
void Update()
{
float horizontalInput Input.GetAxis("Horizontal"); // get horizontal input from the player
float verticalInput Input.GetAxis("Vertical"); // get vertical input from the player
// move the player horizontally based on the horizontal input
Vector2 movement new Vector2(horizontalInput * speed, rb.velocity.y);
rb.velocity movement;
// rotate the player horizontally based on the vertical input
anim.SetFloat("Speed", Mathf.Abs(horizontalInput));
// check if the player is jumping and apply force accordingly
if (Input.GetButtonDown("Jump") && rb.isGrounded)
{
rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
anim.SetTrigger("Jump");
}
}
}
In this script, we define three variables: speed
, turnSpeed
, and jumpForce
. We then get references to the player’s Rigidbody2D
and Animator
components in the Start()
function. In the Update()
function, we read the horizontal and vertical input from the player using Input.GetAxis("Horizontal")
and Input.GetAxis("Vertical")
. We then move the player horizontally based on the horizontal input using rb.velocity movement;
, and rotate the player horizontally based on the vertical input using anim.SetFloat("Speed", Mathf.Abs(horizontalInput));
. Finally, we check if the player is jumping using Input.GetButtonDown("Jump")
and apply force to make them jump using rb.AddForce()
.
Adding Animation to Custom Player Controllers
Once you have created a custom player controller script, you can add animation to it to make the movement more fluid and engaging.
Here’s an example of how you can add an animation to a custom player controller script:
csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed 5f; // movement speed of the player
public float turnSpeed 100f; // turn speed of the player
public float jumpForce 10f; // force applied when jumping
public Animator anim;
void Start()
{
anim.SetInteger("State", 0); // set the player's initial state to idle
}
void Update()
{
float horizontalInput Input.GetAxis("Horizontal"); // get horizontal input from the player
float verticalInput Input.GetAxis("Vertical"); // get vertical input from the player
// move the player horizontally based on the horizontal input
Vector2 movement new Vector2(horizontalInput * speed, rb.velocity.y);
rb.velocity movement;
// rotate the player horizontally based on the vertical input
anim.SetFloat("Speed", Mathf.Abs(horizontalInput));
// check if the player is jumping and apply force accordingly
if (Input.GetButtonDown("Jump") && rb.isGrounded)
{
rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
anim.SetTrigger("Jump");
}
}
}
In this script, we added an Animator
component to the player object and got a reference to it in the Start()
function. We then defined one variable: speed
. In the Update()
function, we read the horizontal and vertical input from the player using Input.GetAxis("Horizontal")
and Input.GetAxis("Vertical")
. We then move the player horizontally based on the horizontal input using rb.velocity movement;
, and rotate the player horizontally based on the vertical input using anim.SetFloat("Speed", Mathf.Abs(horizontalInput));
. Finally, we check if the player is jumping using Input.GetButtonDown("Jump")
and apply force to make them jump using rb.AddForce()
, and set the animation accordingly using anim.SetTrigger("Jump");
.
Custom Player Controllers with Animation
Creating a Custom Player Controller Script with Animation
If you want to create a custom player controller with animation, the first step is to write a script that defines the movement rules and references the animation components. You can create a new script in Unity by right-clicking on the Hierarchy and selecting "Create" > "C Script." Once you have created the script, you can add code to define the movement rules and reference the animation components.
Here’s an example of a simple player controller script with animation:
csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed 5f; // movement speed of the player
public float turnSpeed 100f; // turn speed of the player
public float jumpForce 10f; // force applied when jumping
public Animator anim;
public Transform groundCheck;
private Rigidbody2D rb; // reference to the player's rigidbody component
private bool isGrounded; // flag to check if the player is grounded
void Start()
{
rb GetComponent<Rigidbody2D>();
anim GetComponent<Animator>();
isGrounded false;
}
void Update()
{
float horizontalInput Input.GetAxis("Horizontal"); // get horizontal input from the player
float verticalInput Input.GetAxis("Vertical"); // get vertical input from the player
// move the player horizontally based on the horizontal input
Vector2 movement new Vector2(horizontalInput * speed, rb.velocity.y);
rb.velocity movement;
// rotate the player horizontally based on the vertical input
anim.SetFloat("Speed", Mathf.Abs(horizontalInput));
// check if the player is jumping and apply force accordingly
if (Input.GetButtonDown("Jump") && isGrounded)
{
rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
anim.SetTrigger("Jump");
}
}
}
In this script, we added an Animator
component to the player object and got a reference to it in the Start()
function. We also added a Transform
component to the player object and got a reference to it in the Start()
function. We then defined three variables: speed
, turnSpeed
, and jumpForce
. In the Update()
function, we read the horizontal and vertical input from the player using Input.GetAxis("Horizontal")
and Input.GetAxis("Vertical")
. We then move the player horizontally based on the horizontal input using rb.velocity movement;
, and rotate the player horizontally based on the vertical input using anim.SetFloat("Speed", Mathf.Abs(horizontalInput));
. Finally, we check if the player is jumping using Input.GetButtonDown("Jump")
and apply force to make them jump using rb.AddForce()
, and set the animation accordingly using anim.SetTrigger("Jump");
.
Custom Player Controllers with Scripting
Creating a Custom Player Controller Script
If you want to create a custom player controller, the first step is to write a script that defines the movement rules. You can create a new script in Unity by right-clicking on the Hierarchy and selecting "Create" > "C Script." Once you have created the script, you can add code to define the movement rules.
Here’s an example of a simple player controller script:
csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed 5f; // movement speed of the player
public float turnSpeed 100f; // turn speed of the player
public float jumpForce 10f; // force applied when jumping
private Rigidbody2D rb; // reference to the player’s rigidbody component
void Start()
{
rb GetComponent();
}
void Update()
{
float horizontalInput Input.GetAxis("Horizontal"); // get horizontal input from the player
float verticalInput Input.GetAxis("Vertical"); // get vertical input from the player
// move the player horizontally based on the horizontal input
Vector2 movement new Vector2(horizontalInput speed, rb.velocity.y);
rb.velocity movement;
// rotate the player horizontally based on the vertical input
transform.Rotate(0f, verticalInput turnSpeed, 0f);
// check if the player is jumping and apply force accordingly
if (Input.GetButtonDown("Jump") && rb.isGrounded)
{
rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
}
}
}
In this script