- Home /
Minimap issue
Here's the issue: I have a circular minimap. On that minimap, I want to display objective locations. However, if you're far enough away that the objective would normally be drawn past the edge of the minimap, I just want it to put a symbol at the edge of the map. Now, I've already got the trigger set up with the following code:
if((Mathf.Sqrt(((objPos.x) * (objPos.x)) + ((objPos.y) * (objPos.y)))) > 65)//if the objective is more than 65 pixels from the center of the map...
{
var bill = Mathf.Tan(objPos.x/objPos.y);
var x = Mathf.Cos(bill);
var y = Mathf.Sin(bill);
GUI.DrawTexture(Rect(x - 25,Screen.height - y - 25,50,50),HighObjectiveIcon); // draw the actual icon on the screen
}
EDIT: I've updated the code a bit (ignore the awesome naming conventions, I was in a rush, haha). This is CLOSE, but not exact. It now is correctly triggering and redrawing the dot as soon as it goes outside of the circle or comes back into it, but instead of leaving it at the edge, it just draws it in the corner of the screen, haha. Thoughts?
Your code looks weird. I don't think you're telling it to do what you think you're telling it to do.
Answer by syclamoth · Sep 21, 2011 at 09:43 AM
Assuming your minimap is circular (not square like I initially assumed it was), you can try converting the coordinates to a circular coordinate system, and then limiting the radius before converting it back to a cartesian one! All of the functions for this are in the Mathf class- you are already using the one to get the radius in your script above, so all you need is the angle- determined by
Mathf.Atan2(objPos.x, objPos.y);
Now you can change the radius back to 65 (your state limit) and convert them back into the cartesian coordinates that your typical screen outside of submarine sonar displays can understand, using Sin and Cos.
I tried using your method as well, and I ran into the same issue as I did in my updated code above.
Give this a shot- (assu$$anonymous$$g objPos is a vector2)
// define this at the top of your script!
var maxAngle = 65;
var radius = Vector2.Distance(objPos, Vector2.zero);
var angle = $$anonymous$$athf.Atan2(objPos.x, objPos.y);
radius = $$anonymous$$athf.Clamp(0, maxAngle, radius)
Vector2 properPosition = Vector2(radius * $$anonymous$$athf.Cos(angle), radius * $$anonymous$$athf.Sin(angle));
Now use properPosition where you would have used objPos earlier. Your code above is pretty confusing- I'm not actually sure what it does, but I'm pretty sure it's not what you want. This way, you use the same bit of code to display your $$anonymous$$imap icon whether it's on the map or not- you don't need any special cases! (unless you wanted to use a different picture if it was off the map, in which case you would put an if statement in there somewhere changing the texture).
Haha, you'll have to forgive me if I seem a bit slow at this - the only language I've ever written in before was a few months of C#, and I was sorta "throw in" to a game written almost entirely in javascript. In any case, I get that I won't need any more special cases (very awesome, by the way), but are you saying I won't need to define objPos anymore? Because currently I'm using
var objPos = camera.WorldToScreenPoint(ObjectiveHigh.Transform.Position);
This is no longer necessary?
So, basically, this is what I have now: var HighObjectives = GameObject.FindGameObjectsWithTag("ObjectiveHigh"); var LowObjectives = GameObject.FindGameObjectsWithTag("ObjectiveLow");
for (var ObjectiveHigh : GameObject in HighObjectives) {
radius = $$anonymous$$athf.Clamp(0, maxAngle, radius);
Vector2 properPosition = Vector2(radius * $$anonymous$$athf.Cos(angle), radius * $$anonymous$$athf.Sin(angle));
var objPos = camera.WorldToScreenPoint(ObjectiveHigh.transform.position);
if(($$anonymous$$athf.Sqrt(((properPosition.x) * (properPosition.x)) + ((properPosition.y) * (properPosition.y)))) > 65)//if the objective is more than 65 pixels from the center of the map...
{
var bill = $$anonymous$$athf.Tan(objPos.x/objPos.y);
var x = $$anonymous$$athf.Cos(bill);
var y = $$anonymous$$athf.Sin(bill);
GUI.DrawTexture(Rect(x - 25,Screen.height - y - 25,50,50),HighObjectiveIcon); // draw the actual icon on the screen
}
}
And defined above, I have:
var HighObjectiveIcon : Texture;
var LowObjectiveIcon : Texture;
var maxAngle = 65;
var radius = Vector2.Distance(objPos, Vector2.zero);
var angle = $$anonymous$$athf.Atan2(objPos.x, objPos.y);
No, the radius and angle are the polar coordinates deter$$anonymous$$ed by the cartesian coordinates of your $$anonymous$$imap! I was assu$$anonymous$$g that objPos (which, by the way, you definitely still need to define- when I said at the top of the script, I only meant the maxRadius bit, sorry) was defined in terms of the middle of your $$anonymous$$imap. For any of this code to work, you first need to have a coordinate system which is centered on the middle of your $$anonymous$$imap.
if you're more familiar with C#, I'd be more than happy to work in that language ins$$anonymous$$d- I'm much better with it, since it's pretty much all I ever work with!
Ok, so I got the code to fit into $$anonymous$$e just fine, but now it's just not drawing the dot at all! haha. Anyways, here's what I've got.
for (var ObjectiveHigh : GameObject in HighObjectives) {
var objPos = camera.WorldToScreenPoint(ObjectiveHigh.transform.position);//find location
var maxAngle = 65;
var radius = Vector2.Distance(objPos, Vector2.zero);
var angle = $$anonymous$$athf.Atan2(objPos.x, objPos.y);
radius = $$anonymous$$athf.Clamp(0, maxAngle, radius);
var properPosition : Vector2;
properPosition = Vector2(radius * $$anonymous$$athf.Cos(angle), radius * $$anonymous$$athf.Sin(angle));
if(($$anonymous$$athf.Sqrt(((objPos.x) * (objPos.x)) + ((objPos.y) * (objPos.y)))) > 65)//if the objective is more than 65 pixels from the center of the map...
{
var bill = $$anonymous$$athf.Tan(properPosition.x/properPosition.y);
var x = $$anonymous$$athf.Cos(bill);
var y = $$anonymous$$athf.Sin(bill);
GUI.DrawTexture(Rect(x - 25,Screen.height - y - 25,50,50),HighObjectiveIcon); // draw the actual icon on the screen
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612220619im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotation Jerks when Copying From Different Object 1 Answer
Realtime Object Rotation on iPad 0 Answers
Smooth rotation in 90° increments 0 Answers
How To Check If X Rotation Is Between Two Numbers?? 1 Answer
space flying system 3 Answers