Unity Learn home
View Tutorial Content

Movement Basics

Tutorial
Beginner
35 Mins
(418)
Summary
In this assignment we'll add a Rigidbody 2D to our player so that they can moved using physics and write a simple C# script to allow us to move the player around the play field.
Select your Unity version
Last updated: January 21, 2022
5.x
Language
English

1.Controlling the Player

In this assignment we'll add a Rigidbody 2D to our player so that they can moved using physics and write a simple C# script to allow us to move the player around the play field.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)

using UnityEngine; using System.Collections; public class CompletePlayerController : MonoBehaviour { public float speed; //Floating point variable to store the player's movement speed. private Rigidbody2D rb2d; //Store a reference to the Rigidbody2D component required to use 2D Physics. // Use this for initialization void Start() { //Get and store a reference to the Rigidbody2D component so that we can access it. rb2d = GetComponent<Rigidbody2D> (); } //FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here. void FixedUpdate() { //Store the current horizontal input in the float moveHorizontal. float moveHorizontal = Input.GetAxis ("Horizontal"); //Store the current vertical input in the float moveVertical. float moveVertical = Input.GetAxis ("Vertical"); //Use the two store floats to create a new Vector2 variable movement. Vector2 movement = new Vector2 (moveHorizontal, moveVertical); //Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player. rb2d.AddForce (movement * speed); } }

2.Adding Collision

In this assignment we'll add 2D colliders to our game to enable our player to detect collisions with our environment.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)


3.Following the Player with the Camera

In this assignment we'll enable the camera to follow the player around the play field by writing a simple C# script.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)

using UnityEngine; using System.Collections; public class CompleteCameraController : MonoBehaviour { public GameObject player; //Public variable to store a reference to the player game object private Vector3 offset; //Private variable to store the offset distance between the player and camera // Use this for initialization void Start () { //Calculate and store the offset value by getting the distance between the player's position and camera's position. offset = transform.position - player.transform.position; } // LateUpdate is called after Update each frame void LateUpdate () { // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance. transform.position = player.transform.position + offset; } }

Project:
[Archived] 2D UFO
Movement Basics
Movement Basics
General Tutorial Discussion
0
4
1. Controlling the Player
8
8
2. Adding Collision
1
2
3. Following the Player with the Camera
0
0