Undesired movement on entity. Moving instead of only rotating.

Jet always follows the cursor.I couldn’t record the cursor on video but I put the cursor at the bottom end of game window. Desired movement is just to rotate around the cursor not move but it moves.

The following code is system code and video is the mentioned video.

using UnityEngine;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;

public class PlayerRotationSystem : SystemBase
{
    protected override void OnUpdate()
    {
        var mousePosition = Input.mousePosition;
        var cameraRay = Camera.main.ScreenPointToRay(mousePosition);
        var layerMask = LayerMask.GetMask("Floor");

        if (Physics.Raycast(cameraRay, out var hit, 100, layerMask))
        {
            Entities.WithAll<PlayerTag>().ForEach((ref Rotation rot, in Translation pos) =>
            {
                float3 hitPos = hit.point;
                float3 relativePos = hitPos - pos.Value;
                quaternion rotation = quaternion.LookRotationSafe(relativePos, math.up());
                rotation = new quaternion(0, rotation.value.y, 0, rotation.value.w);
                rot.Value = rotation;
            }).Run();
        }
    }
}
2 Likes

You need to go understand what SystemBase does, because from my reading of the above code, that’s likely what is doing the movement.

I’ve never used SystemBase but already a quick glance at the docs reveals it contains a property (or thingy, whatever they call it in entity land) named velocity, and I do not see you setting that to zero above. Have you tried setting velocity to zero?

2 Likes

I already had physic shape and physic body on the entity. I remove physics related stuff and tried again because my entity doesn’t use any physics. But another problem happened. You can see it on the video.

1 Like

up

1 Like

I fixed it! The reason was the difference on y between entity and ray point.hitPos.y = pos.Value.y