BerkinAkkaya

Most Basic 2D Character Controller

Oct 1st, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.57 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(Rigidbody2D))]
  4. public class CharacterController2D : MonoBehaviour {
  5.  
  6.     public float speed;
  7.     Rigidbody2D rb;
  8.  
  9.     void Start () {
  10.         rb = GetComponent<Rigidbody2D>();
  11.     }
  12.  
  13.     void Update ()  {
  14.         rb.velocity = MovementInput * speed;
  15.     }
  16.  
  17.     Vector2 MovementInput{
  18.         get{
  19.             int x = Input.GetKey(KeyCode.D) ? 1  :
  20.                     Input.GetKey(KeyCode.A) ? -1 : 0;
  21.             int y = Input.GetKey(KeyCode.W) ? 1  :
  22.                     Input.GetKey(KeyCode.S) ? -1 : 0;
  23.  
  24.             return new Vector2(x, y);
  25.         }
  26.     }
  27.  
  28.     /* Published for berkinakkaya.wixsite.com/blog */
  29. }
Advertisement
Add Comment
Please, Sign In to add comment