- Home /
Trouble with raycasting
I'm trying to script my AI on a simple way. The AI does a raycast in front, left and right of it. Then it takes a random direction in a way that doesn't contain a "Boundary"-element.
First, my Update() checks if it's time to calculate a new direction. If it is, it calculates the new direction, then it moves to that.
I'm using the following code to move:
Debug.DrawLine(transform.position, transform.position + transform.forward, Color.yellow);
Debug.DrawLine(transform.position, transform.position + transform.right, Color.yellow);
Debug.DrawLine(transform.position, transform.position - transform.right, Color.yellow);
//DEBUGS START AND END POSITION ARE CORRECT
var startTime;
if (Time.time > nextUpdate) {
Debug.Log("New check");
var dirWay = MoveDirection();
//if (dirWay == 0)
//rot = Quaternion.Euler(0, 0, 0);
if (dirWay == 1) {
rot = Quaternion.Euler(0, 90, 0);
}
if (dirWay == 2) {
rot = Quaternion.Euler(0, -90, 0);
}
if (dirWay == 3) { //backwards
rot = Quaternion.Euler(0, 180, 0);
}
nextUpdate = Time.time + walkTime; //for example, 2: Every 2 seconds an update
direction.y = 1;
direction.y = 1.5 - transform.position.y;
transform.rotation = transform.rotation * rot;
transform.position = transform.position + transform.forward + transform.forward;
//Plus 2 * transform.forward because it moves 2 places
}
The function MoveDirection checks for obstacles through raycasting. My AI moves the correct distance in the correct time, but walks through walls. That means my raycasting is wrong. I'm using the following code:
var obstacles = ["Border", "Boundary", "BoundaryFlame"];
var frontAvailable = true;
var leftAvailable = true;
var rightAvailable = true;
var hitFront: RaycastHit;
if (Physics.Raycast(transform.position, transform.position + transform.forward, hitFront, 1.9)) {
for (var i = 0; i < obstacles.length; i++)
{
if (hitFront.collider.gameObject.name.IndexOf(obstacles[i]) > -1)
{
frontAvailable = false;
}
}
}
var hitLeft: RaycastHit;
if (Physics.Raycast(transform.position, transform.position - transform.right, hitLeft, 1.9)) {
for (var j = 0; j < obstacles.length; j++)
{
if (hitLeft.collider.gameObject.name.IndexOf(obstacles[j]) > -1)
{
leftAvailable = false;
}
}
}
var hitRight: RaycastHit;
if (Physics.Raycast(transform.position, transform.position + transform.right, hitRight, 1.9)) {
for (var k = 0; k < obstacles.length; k++)
{
if (hitRight.collider.gameObject.name.IndexOf(obstacles[k]) > -1)
{
rightAvailable = false;
}
}
}
So, am I right that when I want to check 2 units in front of the AI (transform.forward from the AI's point of view, not the global view!), I should use: Physics.Raycast(transform.position, transform.position + transform.forward, hitFront, 1.9)
?
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
static function Raycast (origin : Vector3, direction : Vector3, distance : float = $$anonymous$$athf.Infinity, layer$$anonymous$$ask : int = kDefaultRaycastLayers) : boolean
Physics.Raycast(transform.position, transform.forward, hitFront, 1.9)
Direction is just transform.forward, the way you had it would absolutely do strange things !
am why static raycast?
if he'll do for more than 1 AI all raycasts will use same raycast with same vars in it
well I've been just reading:
If you are talking about this line :
static function Raycast (origin : Vector3, direction : Vector3, distance : float = $$anonymous$$athf.Infinity, layer$$anonymous$$ask : int = kDefaultRaycastLayers) : boolean
that is directly from the API, stating that raycast is a static variable of a built-in class. Y'know, all the commands that make Unity work?!
As mentioned by @Alucardj your second parameter is an addition of your position and the forward. The proper is forward - position or even better only forward. Also, using transform to move is not best approach. You should prefer a character controller.
I've removed the Character Controller to check on a collision (Rigidbody). I wanted to use transform.Lerp to move the AI. Changing to your solution unfortunately still let the AI walk through walls. That should mean the Raycast is still uncorrect, am I right? Could it indeed be the Raycast being static gives problems when I try to do this for 4 AI's (all with the same script).
Your answer
Follow this Question
Related Questions
Disabling a lineRender or Ray when there is no target 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Raycast collided object not transforming when scripted to? 1 Answer
Object reference not set to an instance of an object 1 Answer