Use raycast with blox3


#1

Hi,

Trying to use raycast with blox3:

  • Used debugdrawraycast and physics raycast in update as below but cant see the debug ray and getting an error message.
  • Could someone show me an example of raycasting code in blox3 pls?

Got a feeling its really simple…Thks!

NullReferenceException: Object reference not set to an instance of an object
BloxEngine.BloxBlock.RunBlock () (at D:/DevUnity/_SOURCE/Blox/BloxEngine/Blox/BloxBlock.cs:130)
BloxEngine.BloxBlock.Run () (at D:/DevUnity/_SOURCE/Blox/BloxEngine/Blox/BloxBlock.cs:469)
BloxEngine.BloxEvent.Run () (at D:/DevUnity/_SOURCE/Blox/BloxEngine/Blox/BloxEvent.cs:172)
BloxEngine.BloxEventHandler.RunEvent (BloxEngine.BloxEvent ev, BloxEngine.BloxEventArg[] args) (at D:/DevUnity/_SOURCE/Blox/BloxEngine/Blox/EventHandlers/BloxEventHandler.cs:29)
BloxEngine.BloxEventHandler.RunEvents (System.Collections.Generic.List`1 events, BloxEngine.BloxEventArg[] args) (at D:/DevUnity/_SOURCE/Blox/BloxEngine/Blox/EventHandlers/BloxEventHandler.cs:18)
BloxEngine.Update_BloxEventHandler.Update () (at D:/DevUnity/_SOURCE/Blox/BloxEngine/Blox/EventHandlers/Update_BloxEventHandler.cs:16)


(Leslie Young) #2

What is the GameObjectRef Get Value at... part all about? I think the problem may lay in it.
It helps when you add a Debug Block to see which of the two Block lines gave the problem when you get a useless error message like this. By adding a Debug, with short message like “A” and “B” above each of them you can see which caused the error. The problem here is that an internal error occurred because a param is invalid. I am guessing the array index you are trying to access contains a null value and that is a valid value. So Blox could not provide a better error message.

Here is an example. I use Local Variables since Global Variables can’t be assigned scene objects seeing as Global Variables are stored in a prefab and no in the scene. Also note that just passing an object’s position for the direction field will not work correctly. Direction is calculated by subtracting one vector (position) from another and the normalizing it. I do not normalise since the Debug.DrawLine expect the length present in the vector. (else it would draw it with one unit length and not up the cubes).


#3

Thks for the reply Leslie.

1/ I did try to follow your example but still still cant see the ray. I did check that the ray direction is correct using a debug block. Gonna sound stupid, but do I have to enable drawing in editor? I checked in gizmo but cant find any option regarding raycast

2/ I then tried to use an actual raycast using the Physics.Raycast. Thats when I get the NullReference error again.
3/ i basically just trying to make a gun laser script so that when the raycast touched something, the bullet will move there.


NullReferenceException: Object reference not set to an instance of an object
BloxEngine.BloxBlock.RunBlock () (at D:/DevUnity/_SOURCE/Blox/BloxEngine/Blox/BloxBlock.cs:130)
BloxEngine.BloxBlock.Run () (at D:/DevUnity/_SOURCE/Blox/BloxEngine/Blox/BloxBlock.cs:469)
BloxEngine.BloxEvent.Run () (at D:/DevUnity/_SOURCE/Blox/BloxEngine/Blox/BloxEvent.cs:172)
BloxEngine.BloxEventHandler.RunEvent (BloxEngine.BloxEvent ev, BloxEngine.BloxEventArg[] args) (at D:/DevUnity/_SOURCE/Blox/BloxEngine/Blox/EventHandlers/BloxEventHandler.cs:29)
BloxEngine.BloxEventHandler.RunEvents (System.Collections.Generic.List`1 events, BloxEngine.BloxEventArg[] args) (at D:/DevUnity/_SOURCE/Blox/BloxEngine/Blox/EventHandlers/BloxEventHandler.cs:18)
BloxEngine.Update_BloxEventHandler.Update () (at D:/DevUnity/_SOURCE/Blox/BloxEngine/Blox/EventHandlers/Update_BloxEventHandler.cs:16)


(Leslie Young) #4

You can see the Debug ray in the scene view or when you activate the gizmos option in the game view.

I see the problem though. Something is going wrong with the “out” param hitInfo of the Raycast Block. I’ll investigate.


(Leslie Young) #5

I found the problem and will push an update soon. Also have a look at this example on how to correctly use Raycast. You need to put it in an IF since it returns True if it hits something, else False.

The LayerMask is a bit tricky. Easiest is to simply pass -1 which means to include everything in the raycast test. Using '0` will always result in no ray hits.

You can also test for a specific Layer. Easiest is to use its name. Just make sure to include Blocks from LayerMask so you can get the layer from the name and then apply some bitwise maths to turn the layer into a layer mask. The [1 << layer] makes use of an Integer, not Float.

The Normalise Block is under Vector3.

My Debug statement (inside IF) is … RaycastHit-> transform -> gameObject -> name since the variable “hit” will be of type “RaycastHit” after the Raycast is done.


#6

Thks so much Leslie!

It works and now I have learned more about raycast layermask and bitwise. Feeling smarter now :slight_smile: haha