forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBallKickingSystem.cs
More file actions
52 lines (47 loc) · 1.77 KB
/
Copy pathBallKickingSystem.cs
File metadata and controls
52 lines (47 loc) · 1.77 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
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.NetCode;
using Unity.Transforms;
using KickBall;
using Unity.Physics;
using Unity.Physics.Extensions;
namespace Kickball
{
[UpdateInGroup(typeof(PredictedSimulationSystemGroup))]
public partial struct BallKickingSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<BallConfig>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var ballConfig = SystemAPI.GetSingleton<BallConfig>();
foreach (var (input, playerTransform) in
SystemAPI.Query<RefRO<PlayerInput>, RefRO<LocalTransform>>()
.WithAll<Player, Simulate>())
{
if (!input.ValueRO.KickBall.IsSet)
{
continue;
}
foreach (var (velocity, mass, ballTransform) in
SystemAPI.Query<RefRW<PhysicsVelocity>, RefRO<PhysicsMass>, RefRO<LocalTransform>>()
.WithAll<Ball>())
{
float distSQ = math.distancesq(playerTransform.ValueRO.Position, ballTransform.ValueRO.Position);
if (distSQ <= ballConfig.KickingRangeSQ)
{
var playerToBall = ballTransform.ValueRO.Position.xz - playerTransform.ValueRO.Position.xz;
var impulse = (math.normalizesafe(playerToBall) * ballConfig.KickForce).xxy;
impulse.y = 0;
velocity.ValueRW.ApplyLinearImpulse(mass.ValueRO, impulse);
}
}
}
}
}
}