using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
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;
[UpdateBefore(typeof(TransformSystemGroup))]
public partial struct FartManMovementSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
FartManMoveInputComponent playerInput = SystemAPI.GetSingleton<FartManMoveInputComponent>();
Debug.Log(playerInput.rotationVector);
new FartDistanceJob { playerInput = playerInput }.Run();
JobHandle fartJob = new FartManMoveJob { playerInput = playerInput }.Schedule(state.Dependency);
fartJob.Complete();
JobHandle rotateJob = new FartManRotateJob { playerInput = playerInput }.Schedule(state.Dependency);
rotateJob.Complete();
SystemAPI.SetSingleton(new FartManMoveInputComponent
{
fartPower = 0,
fartPowerMuliplyer = SystemAPI.GetSingleton<FartManMoveInputComponent>().fartPowerMuliplyer,
rotationVector = 0,
rotationPower = SystemAPI.GetSingleton<FartManMoveInputComponent>().rotationPower,
goodDistance = false
});
}
}
public partial struct FartManMoveJob : IJobEntity
{
public FartManMoveInputComponent playerInput;
[BurstCompile]
public void Execute(BodyTagComponent body, ref PhysicsVelocity velocity, ref PhysicsMass mass, ref LocalTransform transform)
{
if (playerInput.fartPower > 0 && playerInput.goodDistance)
{
velocity.ApplyLinearImpulse(mass, transform.Up() * playerInput.fartPower);
}
}
}
public partial struct FartManRotateJob : IJobEntity
{
public FartManMoveInputComponent playerInput;
[BurstCompile]
public void Execute(HeadTagComponent head, ref PhysicsVelocity velocity, ref PhysicsMass mass, ref LocalTransform transform)
{
if (playerInput.rotationVector != 0)
velocity.ApplyLinearImpulse(mass, transform.Right() * playerInput.rotationVector * playerInput.rotationPower);
}
}
public partial struct FartDistanceJob : IJobEntity
{
public FartManMoveInputComponent playerInput;
[BurstCompile]
public void Execute(BodyTagComponent body, ref PhysicsVelocity velocity, ref PhysicsMass mass, ref LocalTransform transform)
{
EntityQueryBuilder builder = new EntityQueryBuilder(Allocator.Temp).WithAll<PhysicsWorldSingleton>();
EntityQuery singletonQuery = World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntityQuery(builder);
var collisionWorld = singletonQuery.GetSingleton<PhysicsWorldSingleton>().CollisionWorld;
singletonQuery.Dispose();
RaycastInput input = new RaycastInput()
{
Start = transform.Position,
End = -transform.Up() * playerInput.fartPower,
Filter = new CollisionFilter()
{
BelongsTo = 3,
CollidesWith = 6
}
};
Unity.Physics.RaycastHit hit = new Unity.Physics.RaycastHit();
if (playerInput.fartPower > 0 && collisionWorld.CastRay(input, out hit))
{
playerInput.goodDistance = true;
}
}
}
Hi everyone. I made FartDistanceJob to detect if there is ground under my player, using Unity docs.
But it throws me this exeption: InvalidOperationException: The previously scheduled job ExportPhysicsWorld:CheckDynamicBodyIntegrity reads from the ComponentTypeHandle<Unity.Physics.PhysicsVelocity> CheckDynamicBodyIntegrity.JobData.PhysicsVelocityType. You are trying to schedule a new job FartDistanceJob, which writes to the same ComponentTypeHandle<Unity.Physics.PhysicsVelocity> (via FartDistanceJob.JobData.__TypeHandle.__Unity_Physics_PhysicsVelocity_RW_ComponentTypeHandle). To guarantee safety, you must include ExportPhysicsWorld:CheckDynamicBodyIntegrity as a dependency of the newly scheduled job.
I tried schedule it like this:
JobHandle distanceJob = new FartDistanceJob { playerInput = playerInput }.Schedule(state.Dependency);
distanceJob.Complete();
But it says: InvalidOperationException:
The UNKNOWN_OBJECT_TYPE has been declared as [WriteOnly] in the job, but you are reading from it.
And
UnityException: GetOrCreateTrackerHandleImpl can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
How can I solve this and check ground existence

