Back to Browse

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

312 views
Nov 7, 2021
8:03

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

Download

1 formats

Video Formats

360pmp416.8 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

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