It works fine for the origin entity, but for other entities the collider appears to be offset from the origin in the direction of the position vector.
The offset seems to increase with distance from the origin to entity or/and distance from camera to entity.
What cause the offset?
Or if you know the best practice of clicking to select entities, please let me know.
figure.
1, 2 : Collider region of the spherical entity at (3,3,0). It seems to be shifted to the upper right.
3 : The offset increases as the camera moves away.
What do you mean “at individual spheres or at the world”?
Thank you for show me the sample project.
I see it, it seems to do same things, especially struct Pick : IJob
But my project is much more simple than the sample.
I want to click entity and add or remove tag component of it, so at first I only try to pick entity by clicking.
The figure in the first post shows how the mouse may point to an entity but not be a hit, or it may not point to an entity but be a hit.
The sample project can be picked entity when click anywhere of the entity correctly but my project return Incorrect result.
I show my source code.
I want to get the entity in not only Entities.ForEach but also MonoBehaviour script.
So in the sample project the code implemented as Job System but in my project it’s implemented as MonoBehaviour script.
using UnityEngine;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
public class ECSRaycaster : MonoBehaviour
{
void Update()
{
UnityEngine.Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float rayDistance = 100f;
Entity hitEntity = Raycast(ray.origin, ray.direction * rayDistance);
if (hitEntity != Entity.Null)
{
Debug.Log("Hit");
} else {
Debug.Log("No Hit");
}
}
// copy from: https://docs.unity3d.com/Packages/com.unity.physics@0.4/manual/collision_queries.html#ray-casts
public Entity Raycast(float3 RayFrom, float3 RayTo)
{
var physicsWorldSystem = World.DefaultGameObjectInjectionWorld.GetExistingSystem<Unity.Physics.Systems.BuildPhysicsWorld>();
var collisionWorld = physicsWorldSystem.PhysicsWorld.CollisionWorld;
RaycastInput input = new RaycastInput()
{
Start = RayFrom,
End = RayTo,
Filter = new CollisionFilter()
{
BelongsTo = ~0u,
CollidesWith = ~0u, // all 1s, so all layers, collide with everything
GroupIndex = 0
}
};
Unity.Physics.RaycastHit hit = new Unity.Physics.RaycastHit();
bool haveHit = collisionWorld.CastRay(input, out hit);
if (haveHit)
{
// see hit.Position
// see hit.SurfaceNormal
Entity e = physicsWorldSystem.PhysicsWorld.Bodies[hit.RigidBodyIndex].Entity;
return e;
}
return Entity.Null;
}
}
Ignore my question, you are querying the world, it’s clear from the code. Your code seems fine. It could be that the setup is wrong, for example in your PhysicsShapeAuthoring is there an additional transform on the sphere collider? For example, if you put center of the sphere a little bit offset, it will add that transform to the entity transform and your collider will be a little offset.
If you turn on debug display code in the Physics Scene Basic Elements prefab that comes with every scene in the samples (specifically turn on Draw Colliders on Physics Settings) you can see where physics sees your colliders and if they are offset a bit.
I haven’t moved the Sphere Collider.
I’ve checked the collider using Physics Debug Display and it doesn’t seem to have an offset (I’m really surprised).
I changed the component from a sphere collider to a Physics Shape and Physics Body, but nothing happens.
Then check the ray by adding the following code: Debug.DrawRay (ray.origin, ray.direction * rayDistance, Color.red); It looks like it hits a collider, but it’s not a hit.
Although the post is old, I came across it while searching for a solution to a similar problem. The solution for me was to type the following code for RaycastInput → End (note - in the scene preview, Debug.DrawRay is totally unlogic, but the result is correct (feature logic is working)):