Movement Script for 2d and 3d games

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);
}
}
}
}

Couple things here:

  1. Please post your code using code tags from the text ribbon. It is much easier to read than what you posted.
  2. Is there a question? You just posted some code and didnt elaborate on anything.
1 Like

Neither does the question topic include a question…

1 Like