[SOLVED]how to find a point by angle

Hi Unity Forum,

I’m having trouble instantiating bullets from a changing point in two dimensions. Here’s the situation.

I have a specific point at which a bullet fires from. This point needs to change though, because the bullet increases in size (it’s like a lazer, and thus the bullet gets bigger, but looks as if it’s extending from the gun). Now if my gun is pointed south (negative Y), then there is no issue, as I’m changing the point to go lower based on the size of the lazer. The issue is, as soon as I rotate the gun so it’s not facing directly down, it won’t work. It will still move the lazer down, as opposed to moving the lazer in the direction of the angle it’s pointed in. I’ve been using this function to find the point…

(x,y) = (x + d * sin(a), y + d * cos(a))

the point on the left side is what I want…the d is the size of the lazer object (and the distance I want the point to be from where the lazer was originally created) and a is the angle. This unforutnately doesn’t work. For a better description of this function, I found it here…

Thanks in advance

Well, you could just put an empty on one side of the laser, attach that empty to the gun barrel, rotate it so that its +z is pointing out of the barrel, and then just scale the z value in order to resize the laser… It’ll automatically adjust with rotation, as it’ll be rotating with the gun.

Wouldn’t there still be an issue with scaling the lazer, as it would be scaled from the midpoint, extending equally in both sides of its Z, and thus the lazer would come out the back end of the gun. This is why the point that I have the lazer sitting at needs to change. Unless, by creating an empty object on the lazer and there is some way to scale in only one direction…but I don’t know how to do that. I would prefer to do this using math, as then I don’t have empty game objects floating around, but if that’s the only way, then I’ll need to know a new way to scale an object.

Thanks

No, it wouldn’t scale from the midpoint. If you scale the empty, it’d scale from the empty’s position.

That didn’t seem to work. If I scale the empty, nothing happens, because it’s empty. There is no mesh to scale. Although scaling the empty AND the lazer made it scale differently, I could still find no solution there. I got some screens to show what’s happening.

196978--7163--$lazerworking_574.png
196978--7164--$lazernotworking_749.png
196978--7165--$lazerissue_254.png

So, to solve the issue (without using the empty on the lazer) I would have to use a math formula to find the point at which it needs to be created.

This formula will not work for some reason. It creates it at a point with the correct distance, but the angle is off.

Does this show a better representation of what is going on?

196979--7166--$solution_195.png
196979--7167--$mysolution_780.png

I hope this helps! :slight_smile:

EDIT: I’ve updated the package, you should be able to download it now.

197022–7169–$lasers_157.zip (8.75 KB)

Umm… After positioning it, you need to put the laser inside the empty, not the empty inside the laser.

Thanks Gargerath and Flynn. After looking at Flynn’s example I was able to figure out a solution. I couldn’t use raycasting because I don’t have a definite point at which to hit…so I created an empty on the gun itself. Then, as the lazer increased in size, I adjusted the local y position of the empty on the gun. This gave me the point I needed.