Last active
November 5, 2020 19:50
-
-
Save instance-id/a2fac0c9f004ef426ec8401b47ff6277 to your computer and use it in GitHub Desktop.
Switching from setting position to applying velocity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Burst; | |
using Unity.Collections; | |
using Unity.Entities; | |
using Unity.Jobs; | |
using Unity.Mathematics; | |
using Unity.Physics; | |
using Unity.Physics.Extensions; | |
using Unity.Physics.Systems; | |
using Unity.Transforms; | |
using UnityEngine; | |
namespace instance.id.ECS | |
{ | |
[UpdateBefore(typeof(BuildPhysicsWorld))] | |
public class VehicleMovementSystem : JobComponentSystem | |
{ | |
private EntityQuery vehicleMoveQuery; | |
[BurstCompile] | |
private struct VehicleMoveJob : IJobChunk | |
{ | |
[ReadOnly] public float dT; | |
[ReadOnly] public float speedMultiplier; | |
public Unity.Entities.ComponentTypeHandle<Rotation> rotationType; | |
public Unity.Entities.ComponentTypeHandle<Translation> translationType; | |
public Unity.Entities.ComponentTypeHandle<PhysicsVelocity> physicsVelocityData; | |
[ReadOnly] public Unity.Entities.ComponentTypeHandle<PhysicsMass> physicsMassData; | |
[ReadOnly] public Unity.Entities.ComponentTypeHandle<VehicleTag> vehicleTagType; | |
[ReadOnly] public Unity.Entities.ComponentTypeHandle<MoveSpeed> vehicleSpeedType; | |
[ReadOnly] public Unity.Entities.ComponentTypeHandle<WaypointData> vehicleDataType; | |
[ReadOnly] public Unity.Entities.ComponentTypeHandle<SettingData> vehicleSettingsType; | |
[ReadOnly] public Unity.Entities.ComponentTypeHandle<WPMoveStatus> vehicleMoveStatusType; | |
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) | |
{ | |
var chunkRotation = chunk.GetNativeArray(rotationType); | |
var chunkPosition = chunk.GetNativeArray(translationType); | |
var chunkPMD = chunk.GetNativeArray(physicsMassData); | |
var chunkPVD = chunk.GetNativeArray(physicsVelocityData); | |
var chunkSetting = chunk.GetNativeArray(vehicleSettingsType); | |
var chunkVehicleData = chunk.GetNativeArray(vehicleDataType); | |
var chunkVehicleSpeed = chunk.GetNativeArray(vehicleSpeedType); | |
var chunkMoveStatus = chunk.GetNativeArray(vehicleMoveStatusType); | |
for (var i = 0; i < chunk.Count; i++) | |
{ | |
var rotation = chunkRotation[i]; | |
var position = chunkPosition[i]; | |
var pmd = chunkPMD[i]; | |
var pvd = chunkPVD[i]; | |
var vehicleSetting = chunkSetting[i]; | |
var vehicleData = chunkVehicleData[i]; | |
var vehicleStatus = chunkMoveStatus[i]; | |
var vehicleSpeed = chunkVehicleSpeed[i]; | |
if (vehicleStatus.movementStatus != MovementStatus.Moving) continue; | |
float3 translation = position.Value; | |
var direction = vehicleData.currentWaypoint.Position - translation; | |
var lookRotation = quaternion.LookRotation(direction, new float3(0, 1, 0)); | |
rotation.Value = math.slerp(rotation.Value, lookRotation, vehicleSetting.CurrentTurnSpeed); | |
float3 impulse = dT * vehicleSpeed.speed * speedMultiplier * direction; | |
ComponentExtensions.ApplyImpulse(ref pvd, pmd, position, rotation, impulse, translation); | |
chunkPVD[i] = pvd; | |
chunkRotation[i] = rotation; | |
// chunkPosition[i] = position; | |
// position.Value += dT * vehicleSpeed.speed * lookRotation; | |
} | |
} | |
} | |
protected override JobHandle OnUpdate(JobHandle inputDeps) | |
{ | |
var speedMultiplier = VehicleController.Instance.speedMultiplier; | |
var rotationType = GetComponentTypeHandle<Rotation>(); | |
var translationType = GetComponentTypeHandle<Translation>(); | |
var physicsVelocityData = GetComponentTypeHandle<PhysicsVelocity>(); | |
var physicsMassData = GetComponentTypeHandle<PhysicsMass>(true); | |
var vehicleTagType = GetComponentTypeHandle<VehicleTag>(true); | |
var vehicleSpeedType = GetComponentTypeHandle<MoveSpeed>(true); | |
var vehicleDataType = GetComponentTypeHandle<WaypointData>(true); | |
var vehicleSettingsType = GetComponentTypeHandle<SettingData>(true); | |
var vehicleMoveStatusType = GetComponentTypeHandle<WPMoveStatus>(true); | |
var job = new VehicleMoveJob | |
{ | |
dT = UnityEngine.Time.deltaTime, | |
rotationType = rotationType, | |
vehicleTagType = vehicleTagType, | |
physicsMassData = physicsMassData, | |
physicsVelocityData = physicsVelocityData, | |
speedMultiplier = speedMultiplier, | |
translationType = translationType, | |
vehicleDataType = vehicleDataType, | |
vehicleSpeedType = vehicleSpeedType, | |
vehicleSettingsType = vehicleSettingsType, | |
vehicleMoveStatusType = vehicleMoveStatusType | |
}; | |
return job.Schedule(vehicleMoveQuery, inputDeps); | |
} | |
protected override void OnCreate() | |
{ | |
base.OnCreate(); | |
vehicleMoveQuery = GetEntityQuery( | |
typeof(Rotation), | |
typeof(Translation), | |
ComponentType.ReadOnly<MoveSpeed>(), | |
ComponentType.ReadOnly<VehicleTag>(), | |
ComponentType.ReadOnly<SettingData>(), | |
ComponentType.ReadOnly<WaypointData>(), | |
ComponentType.ReadOnly<WPMoveStatus>()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment