Movement Script for 2D games
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
public Rigidbody2D rb; //the rigidbody refrence will be for the player you want to move
public Vector2 vectorX; //keep the y value to be 0
public Vector2 vectorY; // keep the x value to be 0
// Update is called once per frame
void Update()
{
if (vectorX.x > 0) //not that important, important only if you changing the value in game of the vector x to be negetive by mistake
{
if (Input.GetKey(βdβ))
{
rb.AddForce(vectorX);
}
if (Input.GetKey(βaβ))
{
rb.AddForce(-vectorX);
}
}
else if(vectorX.x<0)
{
if (Input.GetKey(βdβ))
{
rb.AddForce(-vectorX);
}
if (Input.GetKey(βaβ))
{
rb.AddForce(vectorX);
}
}
if(vectorY.y>0)
{
if (Input.GetKey(βsβ))
{
rb.AddForce(-vectorY);
}
if (Input.GetKey(βwβ))
{
rb.AddForce(vectorY);
}
}
if (vectorY.y < 0)
{
if (Input.GetKey(βwβ))
{
rb.AddForce(-vectorY);
}
if (Input.GetKey(βsβ))
{
rb.AddForce(vectorY);
}
}
}
}