Basic 2D Character Movement in Unity - Unity C# Tutorial (Script in Description)

Basic 2D Character Movement in Unity - Unity C# Tutorial (Script in Description)

Channel:
Subscribers:
12,600
Published on ● Video Link: https://www.youtube.com/watch?v=n_Lfg-k9ry0



Category:
Tutorial
Duration: 8:03
156 views
4


In this Unity tutorial, I teach you guys how to do basic 2D character movement in Unity. This includes allowing the player to move forward, back, left, and right on a 2D or 3D space.

C# Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
public float movementSpeed;
public Rigidbody playerR;
public Transform playerT;
public Animator playerA;

void Update(){
if(Input.GetKey(KeyCode.W)){
playerR.velocity = transform.forward * movementSpeed * Time.deltaTime;
playerA.SetTrigger("Run");
playerA.ResetTrigger("Idle");
}
if(Input.GetKeyUp(KeyCode.W)){
playerA.SetTrigger("Idle");
playerA.ResetTrigger("Run");
}
if(Input.GetKey(KeyCode.D)){
playerR.velocity = transform.right * movementSpeed * Time.deltaTime;
playerA.SetTrigger("Run");
playerT.localScale = new Vector3(1, 1, 1);
playerA.ResetTrigger("Idle");
}
if(Input.GetKeyUp(KeyCode.D)){
playerA.SetTrigger("Idle");
playerA.ResetTrigger("Run");
}
if(Input.GetKey(KeyCode.S)){
playerR.velocity = -transform.forward * movementSpeed * Time.deltaTime;
playerA.SetTrigger("Run");
playerA.ResetTrigger("Idle");
}
if(Input.GetKeyUp(KeyCode.S)){
playerA.SetTrigger("Idle");
playerA.ResetTrigger("Run");
}
if(Input.GetKey(KeyCode.A)){
playerR.velocity = -transform.right * movementSpeed * Time.deltaTime;
playerA.SetTrigger("Run");
playerT.localScale = new Vector3(-1, 1, 1);
playerA.ResetTrigger("Idle");
}
if(Input.GetKeyUp(KeyCode.A)){
playerA.SetTrigger("Idle");
playerA.ResetTrigger("Run");
}
}
}

#Unity

If this helped you out and you want more game development Unity tutorials in the future - be sure to like, comment, share, and subscribe!

Follow me on Twitter:
https://twitter.com/omogonix

Try out my games:
https://omogonixlachlan.itch.io

Forgehub:
https://www.forgehub.com/members/omogonix.87700/

Subscribe to my Second Channel:
https://www.youtube.com/channel/UCkylkvUtpvbHfafGfIdMLBQ/featured

Join my Discord:
https://discord.gg/a88vmGD

Facebook Page:
https://www.facebook.com/Omogonix/

Follow me on Instagram:
https://www.instagram.com/uwugonixhalo/

Music Used:
Shipwrek & Zookeepers - Ark (NCS Release)
C418 - Blocks
C418 - Strad







Tags:
unity
unity 2d
2d character movement
unity 2d character movement
character movement
2d
basic 2d character movement
unity basic 2d character movement
unity character movement
game development
unity tutorial
tutorial
how to
c#
chsarp
unity c#
unity c# tutorial
unity player movement
unity 2d player movement
game dev tutorial
game development tutorial
how to make a game
how to make a game unity