Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Raycast Command

Discussion in 'Entity Component System' started by Deleted User, Jan 21, 2019.

  1. Deleted User

    Deleted User

    Guest

    Couldn't find much on this but I am trying to schedule raycasts through the ECS, while the raycast happens it only has one coordinate; which comes from the center of the camera...

    Code (CSharp):
    1. NativeArray<RaycastHit> results = new NativeArray<RaycastHit>(1, Allocator.Temp);
    2. NativeArray<RaycastCommand> commands = new NativeArray<RaycastCommand>(1, Allocator.Temp);
    3. Ray temp = Camera.main.ScreenPointToRay(Input.mousePosition);
    4. commands[0] = new RaycastCommand(temp.origin, temp.direction, 2000.0f);
    5. JobHandle handle = RaycastCommand.ScheduleBatch(commands, results, 1);
    6. handle.Complete();
    That is what it is currently, this is working but I don't think the usage of ScreenPointToRay or even Rays is optimal in this section.

    What I had previously was instead of ray, directly used ScreenToWorldPoint, which again probably isn't as optimal.
    Code (CSharp):
    1. Vector3 origin = Camera.main.ScreenToWorld(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z);
    2. Vector3 direction = Camera.main.transform.forward;
    This would work (theres some tweaking to be done for range If i remember, was something I looked at on friday) but it would only fire a ray from the center of the screen (tested with Debug.DrawRay using commands[0].from/direction).

    If theres a better way of processing this raycast command with the mouse that would be good, the question was more; what is different where a ray seems to update the position and the raycast command doesnt?