# Trying to get my AI to turn and face direction they are going

Can’t seem to figure out why I am having such a hard time at getting my AI to look in direction they are moving. It’s hard to show this in code, but basically I have a flocking state machine for my AI and everything is great. I have raycasts to detect if they see or hit a wall, except the raycast is always facing the same spot. If my AI is moving in opposite direction, the raycast is still 180 degrees in the wrong direction. Wondering if anyone knew any solutions. Again, I need my cast to always point in the direction that my AI is going. I would post code, but I really don’t know how that would help but here is my AI class just in case it’s useful to someone. Any help is appreciated thanks in advance.

Function I am calling the raycast in

``````void MessageOtherAI(Vector3 tempTrans1, Vector3 tempTrans2, float tempFloat)
{
RaycastHit hit;

//sees wall
if (Physics.Raycast(tempTrans1, tempTrans2, out hit, tempFloat))
{
Debug.Log ("I see a wall!");
gameObject.renderer.material.color = new Color(0, 0, 1, 1); //blue
Debug.DrawRay (transform.position, directionOfRay * Range, Color.blue);

if(tempFloat < 5f)
{
Debug.Log ("I have hit a wall!");
gameObject.renderer.material.color = new Color(1, 0, 0, 1); //red
Debug.DrawRay (transform.position, directionOfRay * Range, Color.red);

//essentially this sets hitWall to true
}
}

}
``````

Steer function

``````protected virtual Vector3 Steer(Vector3 target, bool slowDown)
{
// the steering vector
Vector3 steer = Vector3.zero;
Vector3 targetDirection = target - transform.position;
float targetDistance = targetDirection.magnitude;

transform.LookAt(target);

if (targetDistance > 0)
{
// move towards the target
targetDirection.Normalize();

// we have two options for speed
if (slowDown && targetDistance < 100f * speed)
{
targetDirection *= (maxSpeed * targetDistance / (100f * speed));
targetDirection *= speed;
}
else
{
targetDirection *= maxSpeed;
}

// set steering vector
steer = targetDirection - rigidbody.velocity;
steer = Limit(steer, maxSteer);
}

return steer;
}
``````

Search function

``````protected virtual void VSearchState()
{
seesWall = false;
hitWall = false;
Transform wall = GameObject.FindWithTag ("SolidObject").transform;

if (movementFlock == true)
{

RaycastHit hit;

Vector3 directionOfRay = transform.TransformDirection(Vector3.forward);

//Debug.DrawRay (transform.position, directionOfRay * Range, Color.red);

if (Physics.Raycast(transform.position, directionOfRay, out hit, Range))
{

if(hit.collider.gameObject.CompareTag("SolidObject"))
{
seesWall = true;
//Debug.DrawRay (transform.position, directionOfRay * Range, Color.green);
MessageOtherAI (transform.position, directionOfRay, Range); //if position is within the raycast

if (Physics.Raycast(transform.position, directionOfRay, out hit, hitWallRange))
{
hitWall = true;
MessageOtherAI (transform.position, directionOfRay, hitWallRange);
}
//set to move in a random direction now...

}

}

Vector3 detectWallHit = wall.transform.position - transform.position;
float distanceFromWall = detectWallHit.magnitude;
if(distanceFromWall < 2f)
{
/*Debug.Log ("TEST");
gameObject.renderer.material.color = new Color(0, 0, 1, 1); //blue*/

}
}

}
``````

Need to know when in the Update() loop these transform calculations are happening, are you making use of LateUpdate() or skipping every other frame ? Some objects like transform forward vectors don’t update when you expect them to (ie setting transform.LookAt() doesn’t actually do anything until the next frame)