#Rotating object depending on terrain under it

1 messages · Page 1 of 1 (latest)

frozen pilot
#

Hello everyone! What are my ways to rotate object depending on the terrain under it in DOTS? So the object will be always perpendicular to the terrain under it?
I have already tried to do this using CollisionWorld.CalculateDistance(), but it seems not to work as it should:
https://www.youtube.com/watch?v=p3G0Ms5Y5SE - here is the video showing how it works in my try. You can see that it is not good at all.
Here is the code I used in this try to do movement and rotation

`public partial struct MovementJob : IJobEntity
{
public float time;
[ReadOnly]public CollisionWorld collisionWorld;
public void Execute(ref LocalTransform transform, in MovementComponent movementComponent)
{
float3 tempDir = movementComponent.target - transform.Position;
tempDir.y = 0;
float3 dir = math.normalize(tempDir);

    if (math.distancesq(movementComponent.target, transform.Position) < 1f)
        return;

    CollisionFilter filter = new CollisionFilter
    {
        BelongsTo = ~0u,
        CollidesWith = 1u << 7,
        GroupIndex = 0
    };
    float3 originPointDistance = transform.Position;
    originPointDistance.y -= 1f;
    var pointDistanceInput = new PointDistanceInput { Filter = filter, Position = originPointDistance, MaxDistance = 1.5f };
    if (collisionWorld.CalculateDistance(pointDistanceInput, out DistanceHit closestHit))
    {
        transform.Position.y = closestHit.Position.y + 1;
        transform.Rotation = quaternion.LookRotation(transform.Forward(), closestHit.SurfaceNormal);
        
    }
    else
    {
        Debug.Log("ERROR: Terrain under unit is not found");
    }
    
    transform.Position += dir * time * movementComponent.speed;
}

}`

Also pinned the .unitypackage with that scene if needed

digital hatch
#

You can use collisionWorld.CastRay and if it hits, the hit.SurfaceNormal is the data you're looking for. This is a vector pointing away from the triangle you're colliding with. Given you have converted the terrain to a MeshCollider.

Then you can do some quaternion math but I have to look that up every time so I don't know how at the moment 😄

frozen pilot
#

I tried that as well, but it was not so good way to do. I mean, if we CastRay from the center of the cylinder. That way, when the cylinder gets close to some roughness of terrain, the ray is casted not particularly where it should be. Happens something like on the picture. And because of that it starts rapidly changing rotation every frame