Search code examples
unity-game-enginevectorlocalspaceraytracing

Unity3D random vector3 in local space


I have to randomize vector3 between -variable and variable in X and Y axis. So Z axis is forward and I need find random vector3 in a circle around that vector, but that all in local space. Creating vector3 and adding it to local space is a bad idea. enter image description here Player can walk and rotate around Y axis and his face can move around X axis.


Solution

  • is this what you want?

    var cam       = Camera.main;
    var distance  = 1000f;
    var tolerance = 1f;
    var offset    = cam.transform.forward * distance;
    var direction = (offset + new Vector3(Random.Range(0f, tolerance), Random.Range(0f, tolerance), 0f)).normalized;
    
    var lineOfSight = new Ray(cam.transform.position, direction);