Raycasting direction from a yet to be instantiated prefab

Hi,

Been breaking my head on this the last few hours. Been googling endlessly, couldn’t find what I needed.

Basically, I’m building a puzzle generator which places 2x1x1 blocks on a 5x5x5 grid. Position is random and rotation is random in increments of 90. Now, because my block has its pivot on the center of 1 half of the block I need to raycast into the other half of the object to check if there’s something in the way which would cause it to be in another object when it’s going to spawn. Alas, that’s not the problem, it’s actually working fine.

However my problem is that I’m having issues getting the right Raycast Vector3 direction. I’m managing the spawning and raycasting from a seperate manager object.

What I’m doing is:

  1. Get random spawn position

Problem > 2. Raycast from random spawn position
to up to check if there’s something ahead

  1. Spawn if there isn’t

  2. repeat if there is

However because I dont have access to the spawned gameobject yet I’m having issues getting the right direction for the Raycast to go.

What’s the best way for me to get the Raycast direction from step 2? I’m out of ideas at this point.

Thanks

In your prefab, the second half will be in some direction from the pivot…left, right, up, down. You can take that direction and multiply it by the rotation to get the direction of raycast. So say the other half is to the right, and you have a Quaternion for the rotation called ‘rot’. You could do:

var dir = rot * Vector3.right;

Then you can use ‘dir’ as the direction in the Raycast().