Posted: 16th Dec 2021 23:05
I am using the raycasting code to determine if an object already exists at the given location:

+ Code Snippet
Function GetObjectIDAtLocation(x, y, z, distance)

    ExitFunction ObjectRayCast(0, x, y-distance, z, x, y, z)
     
EndFunction 0


When the virtual button is pressed a check is made and if the player hasn't spawned a new defence it is created at that location:

+ Code Snippet
function getJoystickButtons()
	if GetVirtualButtonPressed(OBJECT_ID_JOYSTICK_BUTTON)
		objectid as integer
		objectid = GetObjectIDAtLocation(cursor.x, 0, cursor.z, 0.50)
		consoleLog(2, "Object ID: " + str(objectid), 300, 170)
		if objectid < 99999
			createDefense(cursor.x, cursor.z)
		endif
	endif
endfunction


+ Code Snippet
function createDefense(cellx, cellz)
		playerdefense[playerdefensecounter].x = cellx * 1
		playerdefense[playerdefensecounter].z = cellz * 1
		playerdefense[playerdefensecounter].id = CreateObjectSphere(1, 10, 10)
		SetObjectPosition(playerdefense[playerdefensecounter].id, playerdefense[playerdefensecounter].x, 2, playerdefense[playerdefensecounter].z)
		SetObjectColor(playerdefense[playerdefensecounter].id, DEFENSE_COLOR_RED, DEFENSE_COLOR_GREEN, DEFENSE_COLOR_BLUE, 100)
		consoleLog(3, "Object Created: " + str(playerdefense[playerdefensecounter].id), 300, 185)
		consoleLog(4, "Object X: " + str(playerdefense[playerdefensecounter].x), 300, 200)
    		consoleLog(5, "Object Z: " + str(playerdefense[playerdefensecounter].z), 300, 215)
endfunction


The issue I am having is even when a new defense is spawned it still returns that no object is there. If I increase the distance to .6 then it detects the floor as an object and no defenses can be created. Any ideas, I am totally stumped on this one.
Posted: 16th Dec 2021 23:35
make sure y (or any other coord) can't be "inside" an object or it won't get detected.

this:
+ Code Snippet
GetObjectIDAtLocation(cursor.x, 0, cursor.z, 0.50)

...mean's you have a whopping 0.5 world units' worth of a Ray. good luck with that

another thought: you are turning your object(s) collision on via SetObjectCollisionMode(), right?

the ray won't hit it/them, otherwise.

meanwhile, try to use more meaningful thread titles?

i've changed this one so it shows when others search for similar (which we should do before we ask the same questions over and over ).
Posted: 17th Dec 2021 2:14
Please check your original thread where this was discussed, we elaborated this exact issue and I posted a "all-seasons" solution

https://forum.thegamecreators.com/thread/228117#msg2668456
Posted: 17th Dec 2021 9:15
Thread locked.
If you have further questions, then stick with your original thread on the subject.