Forum Discussion

tomotani's avatar
tomotani
MHCP Member
6 months ago

Desktop Editor raycast issues?

Hi everyone,

I’m running into an issue that I thought would be fairly straightforward.

I’m trying to use a laser-pointer–style object to grab and interact with objects for a simple puzzle game. The problem is that my raycast isn’t logging any hits on the tagged objects.

To troubleshoot, I set up a minimal test:

  • Created a cube as my grabble object
  • Attached a raycast to it
  • When grabbed, the raycast should change the color of a simple sphere

But I’m still not seeing any hits registered.

Here’s the simplified script I’m testing with:

import { Raycast, Entity, Color, Component } from 'horizon';

export class LaserColorChanger extends Component<typeof LaserColorChanger> {
  static override typeName = "LaserColorChanger";

  private raycast: Raycast | null = null;

  start() {
    this.raycast = this.entity.getComponent(Raycast);
  }

  update() {
    if (!this.raycast) return;

    const hit = this.raycast.getHit();
    if (hit && hit.entity && hit.entity.name === "TargetSphere") {
      hit.entity.getComponent("MeshRenderer").setColor(Color.red());
      console.log("Hit TargetSphere!");
    }
  }
}

Has anyone else run into this or see what I might be missing?

Thanks in advance!

3 Replies

  • You need to check the hits target type first.

    if (!this.props.raycast) return;
    const hit: RaycastHit | null = this.props.raycast.as(RaycastGizmo).raycast(this.entity.position.get(), this.entity.forward.get(), {
         layerType: LayerType.Both,
         maxDistance: 1
    });
    
    if (hit) {
         if (hit.targetType === RaycastTargetType.Entity) {
              // hit.target is an animated/interactable entity
              }
         else if (hit.targetType === RaycastTargetType.Player) {
              // hit.target is a player
         }
    }

     

  • Also, curious if AI generated this? There is no horizon library. I think you mean 'horizon/core' and there is no Raycast there is RaycastGizmo. There is also no getComponent there is getComponents. So, unless you have access to stuff I do not you will need to make some changes.

  • Yes, I have been trying a number of different ai tools.
    Your reply was helpful, I was able to use it as an example for the ai to check every line of code it wrote against a list of Worlds docs.

    I was able to get it working, the world that I was building i was wonky. I could figure out why trivial things just wouldn't work. I did a work around by duplicating a tutorial world and building off of it and raycasting has worked great since then. 

    Thanks for the help.