I was just wondering if there was a way to find the name of a physics body a 2D raycast is hit by??
For my game. It’s supposed to be one of the mechanics that I wanna implement.
Thanks in advance
I was just wondering if there was a way to find the name of a physics body a 2D raycast is hit by??
For my game. It’s supposed to be one of the mechanics that I wanna implement.
Thanks in advance
@Creator27 - just out of curiosity - is this project of yours a target in a course or your own personal project?
i had this problem too, but there is an info
prop on body that you can use to put a unique name
@skar That works for physics.raycast, but so far I can’t get it to work with Craft physics.raycast. Do you have an example for Craft.
sorry i don’t mess with craft, i’m all 2D
@Creator27 Here’s an example of 2 circles with raycast using information. I’m using “name” and “yPos” to show the name at a specific y position.
viewer.mode=STANDARD
function setup()
p1=physics.body(CIRCLE,40)
p1.type=STATIC
p1.x=WIDTH/2
p1.y=300
p1.name="circle at 300"
p1.yPos=p1.y
p2=physics.body(CIRCLE,60)
p2.type=STATIC
p2.x=WIDTH/2-60
p2.y=600
p2.name="circle at 600"
p2.yPos=p2.y
x=0
v=1
end
function draw()
background(40, 40, 50)
noStroke()
fill(255)
tab=physics.raycastAll(vec2(WIDTH/2,0),vec2(x,HEIGHT))
if tab~=nil then
for a,b in pairs(tab) do
text(b.body.name,60,b.body.yPos)
end
end
ellipse(p1.x,p1.y,80)
ellipse(p2.x,p2.y,120)
x=x+v
if x>WIDTH or x<1 then
v=-v
end
stroke(255)
strokeWidth(2)
line(WIDTH/2,0,x,HEIGHT)
end
@dave1707, I forgot to mention that I’m trying to figure out if you can still find out the object’s name hit by the raycast, even if the physics body of the object is not currently active.
@dave1707, I got mixed up with what I was thinking, sorry about that However, when I tried your code it did not work. Do you think you might know why??
Thanks in advance
@Creator27 It works for me. What’s not working. There should be a line that sweeps back and forth and when it intersects a circle, it will display the circles height.