Determining a vector3 at a random point along the local x axis of a gameObject

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? :stuck_out_tongue:

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!

I’m a bit confused about your directions. You raycast along the local forward axis (so the z-axis). In the description you say you want the point along that line, and not shifted to the left or right… Next thing is you should work with local vectors and don’t fiddle around with world-space positions and change them in one world space axis.

If you really just want what you described in your few points above, just do it like this:

Vector3 lookSpot;

void Check ()
{
    RaycastHit hit;
    if (Physics.Raycast(myTransform.position, myTransform.forward, out hit))
    {
        Vector3 direction = (hit.point - myTransform.position) / 50.0f;
        lookSpot = direction * (Random.Range(2.0f,4.0f) / 5.0f);
        // [...]
    }
}