forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomMotionBehaviour.cs
More file actions
105 lines (92 loc) · 3.46 KB
/
Copy pathRandomMotionBehaviour.cs
File metadata and controls
105 lines (92 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Physics.Systems;
using Unity.Transforms;
using UnityEngine;
using Random = Unity.Mathematics.Random;
public struct RandomMotion : IComponentData
{
public float CurrentTime;
public float3 InitialPosition;
public float3 DesiredPosition;
public float Speed;
public float Tolerance;
public float3 Range;
}
// This behavior will set a dynamic body's linear velocity to get to randomly selected
// point in space. When the body gets with a specified tolerance of the random position,
// a new random position is chosen and the body starts header there instead.
public class RandomMotionBehaviour : MonoBehaviour, IConvertGameObjectToEntity
{
public float3 Range = new float3(1);
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
var length = math.length(Range);
dstManager.AddComponentData<RandomMotion>(entity, new RandomMotion
{
InitialPosition = transform.position,
DesiredPosition = transform.position,
Speed = length * 0.001f,
Tolerance = length * 0.1f,
Range = Range,
});
}
}
[UpdateBefore(typeof(BuildPhysicsWorld))]
public class RandomMotionSystem : JobComponentSystem
{
EntityQuery m_PhysicsGroup;
protected override void OnCreate()
{
m_PhysicsGroup = GetEntityQuery(new EntityQueryDesc
{
All = new ComponentType[] { typeof(PhysicsStep), }
});
}
[BurstCompile]
protected struct RandomMotionJob : IJobForEach<RandomMotion, Translation, PhysicsVelocity, PhysicsMass>
{
public Random random;
public float deltaTime;
public float3 gravity;
public void Execute(ref RandomMotion motion, [ReadOnly] ref Translation position, ref PhysicsVelocity velocity, [ReadOnly] ref PhysicsMass mass)
{
motion.CurrentTime += deltaTime;
random.InitState((uint)(motion.CurrentTime * 1000));
var currentOffset = position.Value - motion.InitialPosition;
var desiredOffset = motion.DesiredPosition - motion.InitialPosition;
// If we are close enough to the destination pick a new destination
if (math.lengthsq(position.Value - motion.DesiredPosition) < motion.Tolerance)
{
var min = new float3(-math.abs(motion.Range));
var max = new float3(math.abs(motion.Range));
desiredOffset = random.NextFloat3(min, max);
motion.DesiredPosition = desiredOffset + motion.InitialPosition;
}
var offset = desiredOffset - currentOffset;
// Smoothly change the linear velocity
velocity.Linear = math.lerp(velocity.Linear, offset, motion.Speed);
if (mass.InverseMass != 0)
{
velocity.Linear -= gravity * deltaTime;
}
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
Random random = new Random();
var physicsStep = m_PhysicsGroup.GetSingleton<PhysicsStep>();
var job = new RandomMotionJob
{
gravity = physicsStep.Gravity,
deltaTime = UnityEngine.Time.fixedDeltaTime,
random = random
};
var jobHandle = job.Schedule(this, inputDeps);
return jobHandle;
}
}