Created
December 1, 2018 13:43
-
-
Save tsubaki/87224860420ee9d2285347bf12f46f97 to your computer and use it in GitHub Desktop.
対象の方を向く
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
using Unity.Jobs; | |
using Unity.Collections; | |
using Unity.Burst; | |
using Unity.Transforms; | |
using Unity.Mathematics; | |
public class FollowTargetSystem : JobComponentSystem | |
{ | |
protected override JobHandle OnUpdate(JobHandle inputDeps) | |
{ | |
inputDeps = new LookTargetJob { | |
positionFromEntity = GetComponentDataFromEntity<Position>(true) | |
}.Schedule(this, inputDeps); | |
return inputDeps; | |
} | |
[BurstCompile] | |
private struct LookTargetJob : IJobProcessComponentData<Target, Position, Rotation> | |
{ | |
[ReadOnly] public ComponentDataFromEntity<Position> positionFromEntity; | |
public void Execute([ReadOnly] ref Target target, [ReadOnly] ref Position pos, [WriteOnly] ref Rotation rot) | |
{ | |
if (!positionFromEntity.Exists(target.Value)) | |
return; | |
var targetPos = positionFromEntity[target.Value]; | |
var diff = targetPos.Value - pos.Value; | |
rot.Value = quaternion.LookRotationSafe(diff, new float3(0, 1, 0)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using Unity.Entities; | |
[DisallowMultipleComponent] | |
public class TargetComponent : ComponentDataWrapper<Target>{} | |
[System.Serializable] | |
public struct Target : IComponentData | |
{ | |
public Entity Value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment