forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomMotionAuthoring.cs
More file actions
102 lines (92 loc) · 3.41 KB
/
Copy pathRandomMotionAuthoring.cs
File metadata and controls
102 lines (92 loc) · 3.41 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
using Unity.Burst;
using Unity.Entities;
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 RandomMotionAuthoring : MonoBehaviour
{
public float3 Range = new float3(1);
}
class RandomMotionAuthoringBaker : Baker<RandomMotionAuthoring>
{
public override void Bake(RandomMotionAuthoring authoring)
{
var length = math.length(authoring.Range);
var transform = GetComponent<Transform>();
var entity = GetEntity(TransformUsageFlags.Dynamic);
AddComponent(entity, new RandomMotion
{
InitialPosition = transform.position,
DesiredPosition = transform.position,
Speed = length * 0.001f,
Tolerance = length * 0.1f,
Range = authoring.Range,
});
}
}
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateBefore(typeof(PhysicsSystemGroup))]
public partial struct RandomMotionSystem : ISystem
{
[BurstCompile]
public partial struct EntityRandomMotionJob : IJobEntity
{
public Random Random;
public PhysicsStep StepComponent;
public float DeltaTime;
public void Execute(ref RandomMotion motion, ref PhysicsVelocity velocity, in LocalTransform transform, in PhysicsMass mass)
{
motion.CurrentTime += DeltaTime;
Random.InitState((uint)(motion.CurrentTime * 1000));
var currentOffset = transform.Position - motion.InitialPosition;
var desiredOffset = motion.DesiredPosition - motion.InitialPosition;
// If we are close enough to the destination pick a new destination
if (math.lengthsq(transform.Position - 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 -= StepComponent.Gravity * DeltaTime;
}
}
}
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<RandomMotion>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
if (!SystemAPI.TryGetSingleton<PhysicsStep>(out var stepComponent))
stepComponent = PhysicsStep.Default;
state.Dependency = new EntityRandomMotionJob
{
Random = new Random(),
DeltaTime = SystemAPI.Time.DeltaTime,
StepComponent = stepComponent
}.Schedule(state.Dependency);
}
}