havenphillip Posted June 18, 2018 Share Posted June 18, 2018 I want to make a horizontal compass that allows the player to navigate towards a goal. How can I set the rotation towards the goal to equal zero so that whenever the player is facing the objective it is set at the middle? Quote Link to comment Share on other sites More sharing options...
macklebee Posted June 19, 2018 Share Posted June 19, 2018 I can't tell from your post if you have a horizontal compass working or not, which is somewhat independent from finding the angle between a player and a target in relation to the player's direction. If you are asking to get the horizontal compass working, calculate the relative angle, and also make the target show up on the compass as well, then this will be a fairly large discussion. And it's another reason why the techass forum is garbage, if this will be a lengthy discussion about multiple things regarding drawing a hud, calculating the relative angle, etc instead of one just concise question about one facet. In any case, if you are just wanting to calculate the relative angle, then you have to calculate the angle between the player and the target based on their global positions, determine the Y-rotation of the player, and then subtract the two. Example script: Script.player = "" --entity "Player Entity" Script.target = "" --entity "Target Entity" Script.angle = 0 function PerfectAngle(pos1,pos2) local dx, dz dx = pos2.x - pos1.x dz = pos2.z - pos1.z return Math:ATan2(dx,dz) end function NormalizeAngle(angle) if angle <= 0 then return angle + 360 else return angle end end function Script:UpdateWorld() self.perfectangle = PerfectAngle(self.player:GetPosition(true),self.target:GetPosition(true)) self.angle = self.player:GetRotation(true).y - self.perfectangle end function Script:PostRender(context) context:SetBlendMode(Blend.Alpha) context:DrawText(string.format("Angle between Player to Target: %.0f",NormalizeAngle(self.angle)),2,22) context:SetBlendMode(Blend.Solid) end Place a pivot into a scene and attach this script. Drag the Player and Target entities into the Script's Property dialog and run the game. 3 Quote Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel Link to comment Share on other sites More sharing options...
havenphillip Posted June 20, 2018 Author Share Posted June 20, 2018 Oh awesome! That's exactly what I was looking for. I only need to do this and some PostRender() stuff but this is right. function NormalizeAngle(angle) if angle <= - 180 then return angle + 360 elseif angle >= 180 then return angle - 360 else return angle + 0 end end Here it is. Working perfectly: UI Elements.zip 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.