I can’t seem to get this right. The cube I made just goes off in a weird direction.
Can anyone share some code?
EDIT: Where does the camera have to be for this to work?
I can’t seem to get this right. The cube I made just goes off in a weird direction.
Can anyone share some code?
EDIT: Where does the camera have to be for this to work?
I have this in my code. (only demonstration, probably unusable for you but I want to highlight the raycast) It’s a hybrid approach. For pure ECS you’d have to use the Physics package.
public static Entity GetEntityFromMouse(LayerMask mask)
{
Entity target = Entity.Null;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000.0f, mask))
{
var go = hit.transform.gameObject;
var comp = go.GetComponent<HybridEntity>();
if (comp != null)
{
target = comp.entity;
}
}
return target;
}
Important part is the raycast so you get a proper position where you actually clicked in 3d space. The hit.position will tell you where the cube should go.
For a pure ECS example, I think there’s a mouse raycast sample in the physics package.
I appreciate the example. I actually figured it out for myself. I was just setting the Vector3.z incorrectly.
I’ll probably be able to share something cool soon.