I want ray cast in 4 directions, instead i am getting this.
This is my script for enemy direction , its just moving forwards and backward. Don't know why other rays are shattered too strong text
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMove : MonoBehaviour
{
public float enemySpeed;
enum Direction { FORWARD,BACKWARD,LEFT,RIGHT};
Direction currentDirection = Direction.LEFT;
bool directionSelected = false;
// Update is called once per frame
void Update()
{
if (directionSelected)
{
switch (currentDirection)
{
case Direction.LEFT:
gameObject.transform.position = gameObject.transform.position + Vector3.left * enemySpeed * Time.deltaTime;
break;
case Direction.RIGHT:
gameObject.transform.position = gameObject.transform.position + Vector3.right * enemySpeed * Time.deltaTime;
break;
case Direction.FORWARD:
gameObject.transform.position = gameObject.transform.position + Vector3.forward * enemySpeed * Time.deltaTime;
break;
case Direction.BACKWARD:
gameObject.transform.position = gameObject.transform.position + Vector3.back * enemySpeed * Time.deltaTime;
break;
}
}
}
private void OnCollisionEnter(Collision collision)
{
directionSelected = false;
Debug.DrawLine(gameObject.transform.position, Vector3.left * 5f, Color.red,1000f);
Debug.DrawLine(gameObject.transform.position, Vector3.right * 5f, Color.red, 1000f);
Debug.DrawLine(gameObject.transform.position, Vector3.forward * 5f, Color.red, 1000f);
Debug.DrawLine(gameObject.transform.position, Vector3.back * 5f, Color.red, 1000f);
while (!directionSelected)
{
Random.seed = System.DateTime.Now.Millisecond;
int checkDirection = Random.Range(1, 4);
Debug.Log(checkDirection);
switch (checkDirection)
{
case 1:
if (!Physics.Raycast(gameObject.transform.position, Vector3.left, 1f))
{
currentDirection = Direction.LEFT;
directionSelected = true;
}
break;
case 2:
if (!Physics.Raycast(gameObject.transform.position, Vector3.right, 1f))
{
currentDirection = Direction.RIGHT;
directionSelected = true;
}
break;
case 3:
if (!Physics.Raycast(gameObject.transform.position, Vector3.forward, 1f))
{
currentDirection = Direction.FORWARD;
directionSelected = true;
}
break;
case 4:
if (!Physics.Raycast(gameObject.transform.position, Vector3.back, 1f))
{
currentDirection = Direction.BACKWARD;
directionSelected = true;
}
break;
}
}
}
}
Answer by Bunny83 · Dec 23, 2018 at 11:51 AM
There are several issues here. First of all Random.Range(1, 4);
will only produce the numbers 1, 2, 3
but never 4 since when using integer bounds the max value is exclusive. You would have to use Random.Range(1, 5);
. But it's more common to use Random.Range(0, 4)
where "4" represents the number of possible outcomes and it would produce 0, 1, 2, 3
The next issue if for some reason no direction is valid, your game will crash / hang because you can never leave the while loop.
A better approach would be to always do those 4 raycasts, push the results into a reused List and then pick a random element from that list. It has the advantage that each direction is only checked once. In your case since you randomly pick a direction it's very likely, if only one direction is valid, that you pick the same invalid direction several times (and in case of no valid you pick them infinitely many times == hang). This also simplifies the detection of "no possible move" because if the list is empty after the 4 raycasts there is no valid direction.
Finally your last issue is just a debugging issue. You do not visualize your raycasts properly. You used Debug.DrawLine. However DrawLine expects two worldspace positions but you passed one position and one direction. So the second point of your line will be basically a point 5 offset from the world origin. Your world origin is most likely somewhere at the 4th column and 3rd row. If you offset this by "5" in each lateral direction you end up at the points where the lines end.
you need to use either:
Debug.DrawLine(transform.position, transform.position + Vector3.left * 5f);
or use DrawRay
Debug.DrawRay(transform.position, Vector3.left * 5f);
The direction detection would be something like this:
private List<Direction> availableDirections = new List<Direction>(4);
private void OnCollisionEnter(Collision collision)
{
availableDirections.Clear();
if (!Physics.Raycast(gameObject.transform.position, Vector3.left, 1f))
availableDirections.Add(Direction.LEFT);
if (!Physics.Raycast(gameObject.transform.position, Vector3.right, 1f))
availableDirections.Add(Direction.RIGHT);
if (!Physics.Raycast(gameObject.transform.position, Vector3.forward, 1f))
availableDirections.Add(Direction.FORWARD);
if (!Physics.Raycast(gameObject.transform.position, Vector3.back, 1f))
availableDirections.Add(Direction.BACK);
if (availableDirections.Count > 0)
{
currentDirection = availableDirections[Random.Range(0, availableDirections.Count)];
}
else
{
// Do something else, no direction to move
}
}
Your answer
Follow this Question
Related Questions
C# Door Script Problem 0 Answers
Raycast hit or miss in the same situation 1 Answer
RayCast From Centre of Camera 1 Answer
Raycast doesn't interact with Tilemap Collider when in a 3D scene. 0 Answers
How to make an object move to the direction in which the user faces using vr gaze interaction? 1 Answer