So im quite new to unity and ive decided to start working on a 2d rpg, using sources from this site ive manged to get a code that gives me grid movement much like that in the older pokemon games. Now i want to introduce collisions and physics to my game but after attaching the rigidbody component and moving around i realise my movement has become very clumsy and well - not nice - to look at. But the real problem is that when ever i walk into an object with a collider - say a tree or something like that - instead of simply refusing to go further it bumbs into the object gets pushed back and repeats and i get stuck in this state. heres my player controller script, i’d appreciate any pointers tips or an entire resolution to my problem THANKS
public class AnnaController : MonoBehaviour {
private Rigidbody2D rgdbdy;
public Animator anmtr;
private static bool playerExists;
private float mSpeed;
public bool overworld;
public Vector2 forwardVector;
public Vector2 mvmntVector;
private Vector3 pos;
private bool hasStepped;
private bool isWalking;
private bool Up;
private bool Down;
private bool Left;
private bool Right;
// Use this for initialization
void Start () {
anmtr = GetComponent<Animator> ();
rgdbdy = GetComponent<Rigidbody2D> ();
mSpeed = 0.75f;
pos = transform.position;
forwardVector = Vector2.zero;
if (overworld) {
if (!playerExists) {
playerExists = true;
DontDestroyOnLoad (transform.gameObject);
} else {
Destroy (gameObject);
}
}
}
// Update is called once per frame
void FixedUpdate () {
anmtr.SetFloat ("ForwardX", forwardVector.x);
anmtr.SetFloat ("ForwardY", forwardVector.y);
Right = false;
Left = false;
Down = false;
Up = false;
if (Input.GetKey (KeyCode.B)) {
mSpeed = 1.25f;
}
else {
mSpeed = 0.5f;
}
if (!Right) {
if (Input.GetAxisRaw ("Horizontal") > 0.5f && transform.position == pos) {
pos += (Vector3.right * 0.32f);
forwardVector = Vector2.right;
}
}
if (!Up) {
if (Input.GetAxisRaw ("Vertical") > 0.5f && transform.position == pos) {
pos += (Vector3.up * 0.32f);
forwardVector = Vector2.up;
}
}
if (!Left) {
if (Input.GetAxisRaw ("Horizontal") < -0.5f && transform.position == pos) {
pos += new Vector3(-0.32f, 0, 0);
forwardVector = Vector2.left;
}
}
if (!Down) {
if (Input.GetAxisRaw ("Vertical") < -0.5f && transform.position == pos) {
pos += new Vector3(0, -0.32f, 0);
forwardVector = Vector2.down;
}
}
transform.position = Vector2.MoveTowards (transform.position, pos, Time.deltaTime * mSpeed);
}
}