RayCastHit requirements

Hi,

Just wondering what components are necessary on an Entity to trigger a RayCastHit.

Here is code if necessary:

    AZ::Vector3 org;
    AZ::TransformBus::EventResult(org, GetEntityId(), &AZ::TransformBus::Events::GetWorldTranslation);
	AzFramework::PhysicsSystemRequests::RayCastConfiguration conf;
	conf.m_origin = org + AZ::Vector3::CreateAxisZ();
	conf.m_direction = AZ::Vector3::CreateAxisZ();
	conf.m_maxHits = 1;
	conf.m_maxDistance = 10.f;

	const AzFramework::PhysicsSystemRequests::RayCastHit* hit;
	AzFramework::PhysicsSystemRequests::RayCastResult res;
	AzFramework::PhysicsSystemRequestBus::BroadcastResult(res, &AzFramework::PhysicsSystemRequestBus::Events::RayCast, conf);
	if (res.GetHitCount() > 0)
	{
		AZ_Printf("RCC", "Found hit");
		hit = res.GetHit(0);
		AZ::EntityId hitEntityId = hit->m_entityId;
		AZ::Entity* e = nullptr;
		AZ::ComponentApplicationBus::BroadcastResult(e, &AZ::ComponentApplicationBus::Events::FindEntity, hitEntityId);

		if (e != nullptr)
		{
			AZ_Printf("RCC", "%s", "Found entity");
		}
		else
		{
			AZ_Printf("RCC", "%s", "Entity not found");
		}
	}

This isn’t causing a RayCastHit in my level even though I can see the Ray intersecting Entities with legacy Mesh Collider components.

Thanks in advance

Hi! I’m swithing to PhysX since 1.20 ver of LY engine.
but if you have valid colliders for meshes that you want to raycasting and it’s still not works for you.
you can also tried to copy file from dev\StarterGame\libs\MaterialEffects\surfacetypes.xml into your project (this is only for legacy phys)

also i have my old code for legacy phys raycast

void SimpleFPSContoller::RayCastForward()
{
	//AzFramework::PhysicsSystemRequest
	AzFramework::PhysicsSystemRequests::RayCastConfiguration rayCastConfiguration;
	rayCastConfiguration.m_origin = m_entity->GetTransform()->GetWorldTranslation();
	//AZ::Transform tm = m_entity->GetTransform()->GetWorldTM().GetInverseFast();
	//AZ::Vector3 forward = (AZ::Vector3(0, 1, 0) * tm).GetNormalized();
	AZ::Vector3 forward = m_entity->GetTransform()->GetWorldTM().GetBasisY();

	rayCastConfiguration.m_direction = forward;
	rayCastConfiguration.m_maxHits = 1;
	rayCastConfiguration.m_ignoreEntityIds = AZStd::vector<AZ::EntityId>{ m_entity->GetId() };
	rayCastConfiguration.m_maxDistance = 200.0f;
	rayCastConfiguration.m_piercesSurfacesGreaterThan = 10000;

	m_rayStartPosition = rayCastConfiguration.m_origin;
	m_rayEndPosition = rayCastConfiguration.m_origin + (rayCastConfiguration.m_direction * rayCastConfiguration.m_maxDistance);
	AzFramework::PhysicsSystemRequests::RayCastResult result;
	EBUS_EVENT_RESULT(result, AzFramework::PhysicsSystemRequestBus, RayCast, rayCastConfiguration);

	if (result.GetHitCount() > 0)
	{
		//AZ_Printf("RayCastResult", "We hit something!", true);
		
		const AzFramework::PhysicsSystemRequests::RayCastHit* hit = result.GetHit(0);
		if (hit->IsValid())
		{
			m_hitPosition = hit->m_position;
			m_hitNormal = hit->m_normal;
			m_hitEntityId = hit->m_entityId;

			EBUS_EVENT_RESULT(m_hitEntity, AZ::ComponentApplicationBus, FindEntity, hit->m_entityId);

			if (m_hitEntity)
				m_hitName = m_hitEntity->GetName();

		}
	}
}

and for PhysX code now looks like this

void SimpleFPSContoller::RayCastForward2()
{
	
	// Code examples: dev\Code\Framework\Tests\Physics\PhysicsGenericInterfaceTests

	Physics::RayCastRequest request;

	request.m_start = m_entity->GetTransform()->GetWorldTranslation();
	request.m_direction = m_entity->GetTransform()->GetWorldTM().GetBasisY();
	request.m_distance = 200.0f;

	// Single world setup
	Physics::RayCastHit hit;
	Physics::WorldRequestBus::BroadcastResult(hit, &Physics::WorldRequests::RayCast, request);
	
	if (hit.m_body != nullptr)
	{
		AZ::EntityId id = hit.m_body->GetEntityId();
		m_hitPosition = hit.m_position;
		m_hitNormal = hit.m_normal;
		m_hitEntityId = hit.m_body->GetEntityId();

		EBUS_EVENT_RESULT(m_hitEntity, AZ::ComponentApplicationBus, FindEntity, m_hitEntityId);

		if (m_hitEntity)
			m_hitName = m_hitEntity->GetName();

	}

	//// #include <AzFramework\Components\CameraBus.h>
	//Camera::CameraRequestBus::Event(GetEntityId(), Camera::CameraRequestBus::Events::MakeActiveView);
	

}