0

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) ?

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Joetjah
  • 6,292
  • 8
  • 55
  • 90

1 Answers1

0

http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

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) ?

you should have Physics.Raycast(transform.position,transform.forward,hitFront, 1.9);

Shredder2500
  • 1,065
  • 2
  • 11
  • 26
  • You can't multiply that. I've tried using `Vector3.Scale` but it was no use neither. – Joetjah Feb 25 '13 at 13:09
  • oh sorry, i wasn't really thinking. first off you dont really need the transform.position, you only need transform.forward * a float. this will just put the position right in front of your tranform how ever far the float value is – Shredder2500 Mar 02 '13 at 21:40
  • Edited the Answer to one that is correct. transform.forward is basically the same as doing transform.rotation * vector3.forward, and vector3.forward is the same as new Vector3(0,0,1) – Shredder2500 Mar 02 '13 at 21:51
  • I'm gonna try this in the evening. It sounds reasonable! – Joetjah Mar 04 '13 at 07:46