- Home /
My Raycast acts like it's hitting something when it isn't
I'm in the middle of developing a horror game with a grid-based movement system. In order to make collision work, I have the player casting rays in four directions and if the ray hits tagged colliders at a certain distance, then the player can no longer move in that direction.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridMovement : MonoBehaviour
{
//Can't move while active
public bool isMoving;
//Can't move forward while active
public bool isMovingForward;
//Can't move back while active
public bool isMovingBack;
//Can't move left while active
public bool isMovingLeft;
//Can't move right while active
public bool isMovingRight;
//Player Position
public Vector3 origPos, targetPos;
//Transition time
public float timeToMove = 0.5f;
//Collider range
public float rayLength = 1.4f;
void Update()
{
//Ray
RaycastHit hit;
//Distance between player and object hit by the Ray
float theDistance;
//Ray forward
Vector3 forward = transform.TransformDirection(Vector3.forward) * 2;
Debug.DrawRay(transform.position, forward, Color.red);
//Ray behind
Vector3 back = transform.TransformDirection(Vector3.back) * 2;
Debug.DrawRay(transform.position, back, Color.red);
//Ray left
Vector3 left = transform.TransformDirection(Vector3.left) * 2;
Debug.DrawRay(transform.position, left, Color.red);
//Ray right
Vector3 right = transform.TransformDirection(Vector3.right) * 2;
Debug.DrawRay(transform.position, right, Color.red);
//if the ray hits an object
if (Physics.Raycast(transform.position,(forward), out hit))
{
theDistance = hit.distance;
//checks the object's tag
if (hit.collider.tag == "Ground")
{
//checks if ibject hit is too close to the player
if (theDistance <= rayLength)
{
isMovingForward = true;
}
else
{
isMovingForward = false;
}
}
}
if (Physics.Raycast(transform.position, (back), out hit))
{
theDistance = hit.distance;
if (hit.collider.tag == "Ground")
{
if (theDistance <= rayLength)
{
isMovingBack = true;
}
else
{
isMovingBack = false;
}
}
}
if (Physics.Raycast(transform.position, (left), out hit))
{
theDistance = hit.distance;
if (hit.collider.tag == "Ground")
{
if (theDistance <= rayLength)
{
isMovingLeft = true;
}
else
{
isMovingLeft = false;
}
}
}
if (Physics.Raycast(transform.position, (right), out hit))
{
theDistance = hit.distance;
if(hit.collider.tag == "Ground")
{
if (theDistance <= rayLength)
{
isMovingRight = true;
}
else
{
isMovingRight = false;
}
}
}
}
private IEnumerator MovePlayer(Vector3 direction)
{
isMoving = true;
float elapsedTime = 0;
origPos = transform.position;
targetPos = origPos + direction;
while(elapsedTime < timeToMove)
{
transform.position = Vector3.Lerp(origPos, targetPos, (elapsedTime / timeToMove));
elapsedTime += Time.deltaTime;
yield return null;
}
transform.position = targetPos;
isMoving = false;
}
//Functions triggered by UI Buttons.
public void MoveForward()
{
if (!isMoving && !isMovingForward)
{
StartCoroutine(MovePlayer(Vector3.forward));
}
}
public void MoveLeft()
{
if (!isMoving && !isMovingLeft)
{
StartCoroutine(MovePlayer(Vector3.left));
}
}
public void MoveBack()
{
if (!isMoving && !isMovingBack)
{
StartCoroutine(MovePlayer(Vector3.back));
}
}
public void MoveRight()
{
if (!isMoving && !isMovingRight)
{
StartCoroutine(MovePlayer(Vector3.right));
}
}
}
When I test it, it seems to work for the most part, I get too close to an object and can't move in that specific direction. However, if I move away from the object, the bool that restricts my movement doesn't switch off unless I move a certain way. Like, I move to a wall in front of me (a basic cube, 2x2 scale; no extra scripts or components attatched), 'isMovingForward' triggers. I move back, it switches off, if I move to the right, no matter how far right I go and the Raycast clearly isn't hitting anything, it doesn't switch off.
Your answer
Follow this Question
Related Questions
Box goes through walls 2 Answers
Prevent random movement allowing movement through obstacles 1 Answer
How to move the main camera in Google Cardboard? 0 Answers
Checking for box collider 2 Answers
Issues with Bullet Ricochet 1 Answer