Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active November 15, 2018 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tsubaki/40383eac7588b99b3e1c1937b234cfb7 to your computer and use it in GitHub Desktop.
Save tsubaki/40383eac7588b99b3e1c1937b234cfb7 to your computer and use it in GitHub Desktop.
Raycast発行(非同期)
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Experimental.AI;
public class RaycastCheck : MonoBehaviour
{
[SerializeField] Transform start, target;
private NavMeshQuery query;
private JobHandle handle;
private NativeArray<NavMeshHit> hits;
private NavMeshLocation location;
private void OnEnable()
{
query = new NavMeshQuery(NavMeshWorld.GetDefaultWorld(), Allocator.Persistent);
hits = new NativeArray<NavMeshHit>(1, Allocator.Persistent);
location = query.MapLocation(start.position, Vector3.up * 10, 0);
}
private void OnDisable()
{
handle.Complete();
hits.Dispose();
query.Dispose();
}
private void Update()
{
handle.Complete();
handle = new RayCastCheckJob()
{
query = query,
location = location,
hits = hits,
startPos = start.position,
targetPos = target.position
}.Schedule(handle);
handle = new LogJob()
{
hits = hits
}.Schedule(handle);
JobHandle.ScheduleBatchedJobs();
}
struct RayCastCheckJob : IJob
{
[ReadOnly]
public NavMeshQuery query;
[WriteOnly]
public NativeArray<NavMeshHit> hits;
public NavMeshLocation location;
public Vector3 startPos, targetPos;
public void Execute()
{
location = query.MoveLocation(location, startPos);
query.Raycast(out NavMeshHit hitcheck, location, targetPos);
hits[0] = hitcheck;
}
}
struct LogJob : IJob
{
[ReadOnly]
public NativeArray<NavMeshHit> hits;
public void Execute()
{
Debug.Log(hits[0].hit);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment