Hi all!
I'm trying to pick a 3D object using the ObjectRayCast() command and I have a problem that I can't solve and I would like to ask for help.
The problem is, if I scale the object up or down after it loaded, ObjectRayCast() is till picking the object's original size and If I scale the object down, I can pick the object even if I click around the object and If I scale the object up I'm unable to pick the object on it sides but if I'm clicking somewhere around it original size (the middle of the object).
I don't know if it a bug or If I'm doing anything wrong.
Anyone could help me please?
This is the code I'm using:
global pickedObjID as integer //ID of picked object
global scaleValue as float = 0.1 //value of scale
SetCameraPosition(1,0,50,-100)
//load cube
LoadObject(1,"cube.fbx")
SetObjectPosition(1,0,0,0)
//text
CreateText(2,"Pick the object using the Left mouse button and use PageUp and PageDown to scale the object")
SetTextSize(2,18)
SetTextPosition(2,0,0)
do
pickObject()
scaleObject()
Sync()
loop
Function pickObject()
If GetRawMouseLeftPressed()
local distance as float
local directionX as float
local directionY as float
local directionZ as float
distance = 3000
directionX = Get3DVectorXFromScreen( GetPointerX(), GetPointerY() ) * distance + GetCameraX(1)
directionY = Get3DVectorYFromScreen( GetPointerX(), GetPointerY() ) * distance + GetCameraY(1)
directionZ = Get3DVectorZFromScreen( GetPointerX(), GetPointerY() ) * distance + GetCameraZ(1)
pickedObjID = ObjectRayCast( 0, GetCameraX(1), GetCameraY(1), GetCameraZ(1), directionX, directionY, directionZ )
EndIf
If pickedObjID > 0 //if object is picked
SetObjectColor(pickedObjID, 122,22,22,255) //set picked object color to red
If GetRawKeyPressed(27) //escape key is pressed deselect object
pickedObjID = 0
EndIf
Else //if no object is picked
SetObjectColor(1,255,255,255,255)
Endif
EndFunction
Function scaleObject()
If pickedObjID > 0
If GetRawKeyPressed(33) //if page up key is pressed, scale it up
SetObjectScalePermanent(pickedObjID,1 + scaleValue, 1 + scaleValue, 1 + scaleValue)
EndIf
If GetRawKeyPressed(34) //if page DOWN key is pressed, scale it down
SetObjectScalePermanent(pickedObjID,1 - scaleValue, 1 - scaleValue, 1 - scaleValue)
EndIf
EndIf
EndFunction
In case you would like to test it, I have also attached an example:
https://forum.thegamecreators.com/attachment/77127
Juts select the box using the left mouse button, the box should turn red when it picked and you can scale it up and down using the PageDown and PageUp keys on your keyboard. Once you have scaled the box, deselect the object by pressing Escape key or click away the object.
Try to select the object again and you should experience the problem.
Thank you