This is for a AI script I am trying to put together for fun. Yeah I am a total noob, only been coding for a couple of weeks, but I’m already biting off more than I can chew, so what yawannafightaboutit?
Anyway,
-
Bot looks forward, throws out a raycast along transform.forward;
-
Registers the hitpoint.
-
Determines the distance of the raycast, and divides it by 50.
-
Determines a random point along that line, no closer than 2/5 of the distance, no further than 4/5 of the distance.
-
Creates a Vector3 at that point, and remembers it in a private variable, to be used for a variety of things (movement, awareness, object creation, various other shenanigans).
Here is the code I am currently using, and please excuse the spaghettist nature, I can up some cleaner code if this is confusing. The function is isolated from “void Update” so that it only needs to be called when the AI needs it.
void Check ()
{
RaycastHit hit; //in the right spot?
float lookFactor;
//Vector3 tooFar;
Vector3 lookSpot;
if (Physics.Raycast(transform.position, myTransform.forward, out hit))
{
safeDistance = hit.distance;//this keeps the safeDistance variable updated
lookFactor = hit.distance / 50F;
//tooFar = hit.point;
lookSpot = new Vector3(Random.Range((lookFactor * 10F), (lookFactor * 40F)), 0, 0);
//lookSpot = (tooNear.x + Random.Range((lookFactor * 10F), (lookFactor * 40F)));
//tooNear.x += Random.Range((lookFactor * 10F), (lookFactor * 40F));
if (!lookSpotted)
{
Instantiate(lookTarget, lookSpot, transform.rotation);
lookSpotted = true;
}
//Instantiate(lookTarget, lookSpot, transform.rotation);
// get this object's position
//pos.x += Random.Range(-range, range); // add a random x offset around it
//var instance : GameObject = Instantiate(thePrefab, pos, transform.rotation);
}
if (safeDistance <= (pathDistance))
{
kindaStuck = true;
//Debug.Log ("Kinda Stuck");//enemy is kinda stuck
}
else
{
kindaStuck = false;
//Debug.Log ("Trippin you out!"); //enemy is free to move
}
}
Currently I am creating a gameObject at the relevant position, but only according to world space not local space. If I can get it to determine the point according to the transform’s local space instead, then I am winning.
Help!