- Home /
Is there a way to use Physics.Raycast to find an Entity?
Is there a way to use Physics.Raycast to find an entity?
Based on the HelloSpawnerProxy.cs code in the HelloECS sample, I'm using a proxy that converts a GameObject into an entity and trying to select an entity with a raycast on a mouse click.
In my scene, a spawner creates a cube entity from a prefab. The raycast from the camera, through the mouse position, toward the entity doesn't hit any entity. If I add a built in 3D cube, the ray cast hits when I click on it. In the 2019.1 beta, there were no proxies and the raycasting worked. I'm using 2019.1.0f2 which seems to require proxies but the raycasting no longer works.
//UnitSpawnerProxy
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
var spawnerData = new UnitSpawner
{
// The referenced prefab will be converted due to DeclareReferencedPrefabs.
// So here we simply map the game object to an entity reference to that prefab.
prefab = conversionSystem.GetPrimaryEntity(prefab)
};
dstManager.AddComponentData(entity, spawnerData);
}
//PlayerInputSystem
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var mousePos = Input.mousePosition;
if (Physics.Raycast(ray, out RaycastHit hit)) {
//do something
}
Your answer
Follow this Question
Related Questions
Touching Falling Objects 1 Answer
Move player with mouse help 0 Answers
How to determine which side of an object the player collided with? 2 Answers
Using RayCast to Get Mouse Input 1 Answer
Simple (?) touch-command on iPhone 2 Answers