Hello
I have spend a bit too long time trying to read documentations, tutorials and videos trying to find how I can my raycasts into jobs. But most examples I found with raycast jobs is done with the unsafe, so I am a bit unsure of how to approach this. According to documentation on unsafe keyword, it will enable pointers and references like in C++.
Do I need to understand C++ pointers and references to be able to use raycast jobs? Just need to know so i know were i should start reading.
C# documentation
https://docs.unity3d.com/Packages/com.unity.physics@0.0/manual/collision_queries.html
Working implementation of raycasting on controller
Work in progress code
public class CharacterControllerSystem : SystemBase
{
private BuildPhysicsWorld buildPhysicsWorldSystem;
protected override void OnCreate()
{
buildPhysicsWorldSystem = World.GetExistingSystem<BuildPhysicsWorld>();
}
protected override void OnUpdate()
{
var rayCaster = new MovementRayCast() { physicsWorld = World.GetExistingSystem<BuildPhysicsWorld>().PhysicsWorld };
float deltaTime = Time.DeltaTime;
Entities.ForEach((ref CharacterDataComponent characterDataComponent, ref Translation translation, ref Rotation rotation, in PlayerControllerComponent playerControllerComponent) =>
{
float3 upwardCorrection = (-characterDataComponent.gravity) * 0.3f;
//Rotation camera independent
if (playerControllerComponent.leftAlt)
{
}
else
{
rotation.Value = quaternion.RotateY(playerControllerComponent.cameraHorizontal);
}
//Driving the gameobject with data from PlayerControllerComponent
float3 vectorRight = math.mul(rotation.Value, math.right());
vectorRight.y = 0;
vectorRight = math.normalizesafe(vectorRight);
translation.Value = translation.Value + (playerControllerComponent.inputVertical * math.forward(rotation.Value)) * 5f * deltaTime;
translation.Value = translation.Value + (playerControllerComponent.inputHorizontal * vectorRight) * 5f * deltaTime;
characterDataComponent.centerPosition = new float3(translation.Value.x, translation.Value.y + characterDataComponent.centerHeight, translation.Value.z);
//jumping TODO add acceleration to jump
if (playerControllerComponent.jump && characterDataComponent.IsGrounded)
{
translation.Value = translation.Value + (-characterDataComponent.gravity) * 60f * deltaTime;
}
//Raycasting end position
var rayCastEnd90Pos = new float3(0, -characterDataComponent.centerHeight, 0);
var rayCastEndSlopePos = math.mul(rotation.Value, rayCastEnd90Pos);
//check for ground. TODO Add somekind of range check to halt the upward movement, mabye use lerp
for (int i = 0; i < 10; i++)
{
float angle = 36 * i;
float3 checkPositionRotation = math.mul(quaternion.RotateY(angle), new float3(0,0,0.25f));
float3 checkPosition = characterDataComponent.centerPosition + checkPositionRotation;
float3 checkPositionEnd = checkPosition + new float3(0, -characterDataComponent.centerHeight, 0);
characterDataComponent.debugPosition = checkPositionEnd;
Unity.Physics.RaycastHit raycastHit = new Unity.Physics.RaycastHit();
//Debug.DrawLine(checkPosition, checkPositionEnd, Color.green,1f);
if (rayCaster.CheckRay(checkPosition, checkPositionEnd, out raycastHit))
{
float distance = math.distance(checkPositionEnd, raycastHit.Position);
if (distance> 0.1f)
{
translation.Value = translation.Value + upwardCorrection * deltaTime;
}
characterDataComponent.IsGrounded = true;
break;
}
else
{
characterDataComponent.IsGrounded = false;
}
}
if (!characterDataComponent.IsGrounded)
{
translation.Value = translation.Value + characterDataComponent.gravity * deltaTime;
}
//check for Step or slope
}).Schedule();
buildPhysicsWorldSystem.AddInputDependencyToComplete(Dependency);
}
private struct MovementRayCast
{
public PhysicsWorld physicsWorld;
public bool CheckRay(float3 startingPosition, float3 endPosition, out Unity.Physics.RaycastHit raycastHit)
{
var ray = new RaycastInput()
{
Start = startingPosition,
End = endPosition,
Filter = new CollisionFilter()
{
GroupIndex = 0,
BelongsTo = 1u << 2,
CollidesWith = 1u << 1
}
};
return physicsWorld.CastRay(ray,out raycastHit);
}
}