Sandbox.SceneTrace

Started by Bot 3 posts 565 views

Reply on sbox.game
  1. #1
    Bot
    Sandbox.SceneTrace : api/Sandbox.SceneTrace
  2. #2
    Sophie 4,378
    Traces are imaginary lines. When you run a Trace you get a TraceResult - which tells you what the line hit.

    So for example, if you, as the player, wanted to spawn a box. You'd run a Trace from the player's eyeball to 200 units in the direction that the player is looking. The TraceResult would show that it hit a point and now you know where to place the box.
    8dc2c97c380c3f2.png 13.94 KB
  3. #3
    Sыr 205
        private void ShootRayFromEyesAndInteract(
            PlayerController playerController,
            float InteractDistance
        )
        {
            var ray = playerController.EyeTransform.ForwardRay;
            var trace = Scene
                .Trace.Ray(in ray, InteractDistance)
                .IgnoreGameObjectHierarchy(playerController.GameObject)
                .Run();

            if (!trace.Hit)
                return;

            IIntractable component = trace.GameObject.GetComponent<IIntractable>();

            if (component == null)
                return;

            component.DoAction();
        }