EDIT: Found the solution. I didn´t know you couldn´t have coordinates as direction. Works just fine now!
Hello guys!
I´m currently working on a top down rpg and i just finished the collision script that´s using ray casting in all four directions you can walk in, before translating the player position in that direction. The problem is that it seems like the only ray that´s hitting anything is the one that is checking if there´s a object in front of the player. The one for the left, right and back are not working at all.
I hope I´ve given you enough information for you to help me with this problem!
Thank you very much!
using UnityEngine;
using System.Collections;
public class playerMoveScript : MonoBehaviour {
int rMove = 0;
int lMove = 0;
int fMove = 0;
int bMove = 0;
int zMove = 0;
int xMove = 0;
float moveSpeed = 7.5f;
void Update () {
int zMove = rMove + lMove;
int xMove = fMove + bMove;
if (xMove != 0 && zMove != 0) {
transform.Translate(Vector3.forward * Time.deltaTime * xMove * moveSpeed / 1.4f);
transform.Translate(Vector3.left * Time.deltaTime * zMove * moveSpeed / 1.4f);
} else {
transform.Translate(Vector3.forward * Time.deltaTime * xMove * moveSpeed);
transform.Translate(Vector3.left * Time.deltaTime * zMove * moveSpeed);
}
if (Input.GetKey(KeyCode.W)) {
if (!Physics.Raycast(transform.position, new Vector3(transform.position.x, transform.position.y, transform.position.z + 2), 2)) {
fMove = 1;
} else {
fMove = 0;
}
} else {
fMove = 0;
}
if (Input.GetKey(KeyCode.S)) {
if (!Physics.Raycast(new Vector3(transform.position.x, transform.position.y, transform.position.z - 0.1f), new Vector3(transform.position.x, transform.position.y, transform.position.z - 2), 2)) {
bMove = -1;
} else {
bMove = 0;
}
} else {
bMove = 0;
}
if (Input.GetKey(KeyCode.A)) {
if (!Physics.Raycast(transform.position, new Vector3(transform.position.x +2, transform.position.y, transform.position.z), 2)) {
lMove = 1;
} else {
lMove = 0;
}
} else {
lMove = 0;
}
if (Input.GetKey(KeyCode.D)) {
if (!Physics.Raycast(transform.position, new Vector3(transform.position.x -2, transform.position.y, transform.position.z), 2)) {
rMove = -1;
} else {
rMove = 0;
}
} else {
rMove = 0;
}
}
}