Instantly share code, notes, and snippets.

Embed
What would you like to do?
対象の方を向く
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