In the attachments I have 2 images. One is of my player cube which is yellow and is shown in the scene view. The other is in the game view. The 3 lines (red, blue and green) are Raycasts that I have defined in a script. They are visible because I used the Debug.DrawRay function. My issue here is that when the player (yellow cube with orange being the front) gets to the top of the staircase he should be unable to walk off the top as the distance is greater than 1. I should also mention that the movement of this cube is 1 unit forward or backward, it rotates 1 unit right or left, and when it is infront of one of the 1 by 1 cubes it will move 1 forward and 1 up, resulting in it scaling the staircase.
I have also attached a video clip link for more precise understanding:
I placed the floor under the layer “Ignore Raycast” so that it wouldn’t affect the player’s movement as it would normally go through the ground once and continue moving.
What I am unable to figure out on my own is if there is a way of NOT putting the floor under the layer “Ignore Raycast”, and HOW I can make it so that the player can’t walk forward if the distance down is GREATER than 1. Here is my move script attached to an empty gameObject which holds the yellow box and the orange “eyes”:
using UnityEngine;
using System.Collections;
public class move : MonoBehaviour {
//Draw the rays?
public bool drawRays;
public float rayCastDist;
void Start () {
}
void Update ()
{
var forward = transform.TransformDirection (Vector3.forward);
var down = transform.TransformDirection (Vector3.down);
var back = transform.TransformDirection (Vector3.back);
//RaycastHit hit;
//Raycasting
if(drawRays)
{
Debug.DrawRay(transform.position, forward * rayCastDist, Color.red);
Debug.DrawRay(transform.position, down * rayCastDist, Color.blue);
Debug.DrawRay(transform.position, back * rayCastDist, Color.green);
}
if(Physics.Raycast (transform.position, forward, rayCastDist))
{
if(Input.GetKeyDown (KeyCode.UpArrow))
{
transform.Translate(0,1,0);
}
}
if(Physics.Raycast (transform.position, back, rayCastDist))
{
if(Input.GetKeyDown (KeyCode.DownArrow))
{
transform.Translate(0,1,0);
}
}
if(Input.GetKeyDown (KeyCode.UpArrow))
{
if(Physics.Raycast (transform.position, down, rayCastDist))
{
transform.Translate (0,-1,0);
}
}
if(Input.GetKeyDown (KeyCode.DownArrow))
{
if(Physics.Raycast (transform.position, down, rayCastDist))
{
transform.Translate (0,-1,0);
}
}
//move forward/backwards
if(Input.GetKeyDown (KeyCode.UpArrow))
transform.Translate (0,0,1);
if(Input.GetKeyDown (KeyCode.DownArrow))
transform.Translate (0,0,-1);
//rotate
if(Input.GetKeyDown (KeyCode.LeftArrow))
transform.Rotate (0,-90,0);
if(Input.GetKeyDown (KeyCode.RightArrow))
transform.Rotate (0,90,0);
//move up/down
if(Input.GetKeyDown (KeyCode.Q))
transform.Translate (0,1,0);
if(Input.GetKeyDown (KeyCode.E))
transform.Translate (0,-1,0);
}
}
Thank you SO much in advance; I have tried and tried but my luck did not exceed me.