Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created December 1, 2018 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/87224860420ee9d2285347bf12f46f97 to your computer and use it in GitHub Desktop.
Save tsubaki/87224860420ee9d2285347bf12f46f97 to your computer and use it in GitHub Desktop.
対象の方を向く
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));
}
}
}
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