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
42 lines (39 loc) · 1.4 KB
/
Copy pathRandomMotionAuthoring.cs
File metadata and controls
42 lines (39 loc) · 1.4 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
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
namespace Common.Scripts
{
// 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 Baker : 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,
});
}
}
}
public struct RandomMotion : IComponentData
{
public float CurrentTime;
public float3 InitialPosition;
public float3 DesiredPosition;
public float Speed;
public float Tolerance;
public float3 Range;
}
}