I'm really new to Unity/C# (I have a bit of experience in GML but far from professional) and I am currently building out my first prototype.
I originally copied THIS script to get a cube that rotates face-to-face along one of its bottom edges.
Currently, the state of the game is the cube "rolling" around on a 10x10 grid. The grid currently has a border of walls. I want the cube to shoot a rayCast in the direction the cube is moving towards, check if there is a wall, and decide whether the cube can move in that direction.
This is the way I had it set up previously:
Vector3 up = new Vector3(0, 0, 0.5f); Vector3 right = new Vector3(.5f, 0, 0); Vector3 down = new Vector3(0, 0, -0.5f); Vector3 left = new Vector3(-.5f, 0, 0); Vector3 currentDirection = Vector3.zero; bool Valid() { Ray myRay = new Ray(transform.position + currentDirection, transform.forward); RaycastHit hit; Debug.DrawRay(myRay.origin, myRay.direction, Color.red); if (Physics.Raycast(myRay, out hit, rayLength)) { if (hit.collider.tag == "Wall") { return false; } } return true; } currentDirection is set to when movement keys "WASD" are pressed. I know one issue that I am running into is that transform.forward does not work because the Y-rotation of my cube never changes.
Gif format not supported so heres a link showcasing the problem: Movement & Clipping: https://i.imgur.com/XxlEx1G.gifv
Overall, I was wondering how to get the raycast to shoot in the appropriate direction based on movement.
Sorry if the answer is incredibly easy and I'm just missing it. Thanks for the help, Kalfin