I am experimenting with raycast with ECS 0.50. I’m trying to see if the mouse position on a surface can be known. I thought if I could reach that surface, maybe I could even create an object on surface too. With the code below, the entity is followed at the intersection of the mouse position and the surface,it is working in play mode but nothing happens when buildandrun is done. Is this a bug? Or are there some hidden option I need to check before buildandrun?
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Physics.Systems;
using Unity.Transforms;
using UnityEngine;
using RaycastHit = Unity.Physics.RaycastHit;
[AlwaysUpdateSystem]
public partial class UnitSelectionSystem : SystemBase
{
private Camera _mainCamera;
private BuildPhysicsWorld _buildPhysicsWorld;
private CollisionWorld _collisionWorld;
protected override void OnCreate()
{
_mainCamera = Camera.main;
_buildPhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
}
protected override void OnUpdate()
{
Entities
.WithAll<Move>()
.WithoutBurst()
.ForEach((ref Translation translation) =>
{
_collisionWorld = _buildPhysicsWorld.PhysicsWorld.CollisionWorld;
var ray = _mainCamera.ScreenPointToRay(Input.mousePosition);
var rayStart = ray.origin;
var rayEnd = ray.GetPoint(100f);
if (Raycast(rayStart, rayEnd, out var raycastHit))
{
var hitEntity = _buildPhysicsWorld.PhysicsWorld.Bodies[raycastHit.RigidBodyIndex].Entity;
translation.Value = raycastHit.Position;
}
}).Run();
code take from this video.