|
So, I have made a map for my game. There are 36 different territories. Each Territory is a different game object. In this map scene, I want it so that when I click one of the territories, i have it so that information pops up on the side. Then when I click another, information pops up for that. I am almost entirely new to scripting. Is there a way to do this without using tags?
(comments are locked)
|
|
You can use OnMouseDown to detect when the object is clicked, and show the info in a GUIText or GUI.Label, like this:
var information: String; // define the text here
private var guiOn = false;
private var rect: Rect;
function OnMouseDown(){
guiOn = true; // enable gui and define position at point clicked
rect = Rect(Input.mousePosition.x, Input.mousePosition.y, 300, 100);
yield WaitForSeconds(5);
guiOn = false;
}
function OnGUI(){
if (guiOn){
GUI.Label(rect, information);
}
}
NOTES: EDITED:
function Update(){
if (Input.GetMouseButtonDown(0)){
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit) && hit.transform.tag=="Block"){
var clickedObj = hit.transform;
// do whatever you want with clickedObj
}
}
}
The problem here is: how to know which information show for each block? To do that, you should have the information stored in each block, or some kind of id number to use as an index and retrieve the information from an array. Both have the same problem: you must attach a script to each block, and write the information or id number for each one in the Inspector. EDITED 2: Since you need only one territory name showing at one time, you should use a single separated script to show the name (attach it to the camera or any other object), and set variables in it when some territory is clicked. This also helps to set the message Box characteristics in the Inspector with the variable style. That's the separated script - let's call it ShowName.js:
static var information: String; // the territory defines this string...
static var rect: Rect; // the message position...
static var endTime: float = 0; // and the time it will appear
var style: GUIStyle; // you can set color, background etc. at the Inspector
function OnGUI(){
if (Time.time aldonaletto , your concept is absolutely correct. but,there is 36 objects in his game if we attach into every object the process will be slow. so, we can tag every objects in a single script. from that we can find object name or object tag in run time. correct?
Sep 09 '11 at 03:43 AM
sriram90
@sriram90: this is possible, but you will have to define the information for each block anyway - I edited my answer to show the approach you've suggested.
Sep 09 '11 at 11:05 AM
aldonaletto
The First one Worked! Kind of.... but my only problem is that, I need a box, because the text kind of fades in into the background... Where and how would I put that? and if I want to switch the territory information I am looking at... What changes would I need. Because, I have territory one selected. When I click it, it says bob. When I click the next one, the bob stays, but fred also pops up for the other one. I want the bob to disappear. Does that make sense??
Sep 10 '11 at 03:32 AM
sketchers1
In this case, maybe the second script could be more indicated. But you can still use the first one, with some changes: you would need a separated script to show the message (it could be attached to the camera or to a empty object) and use some static variables to communicate which is the currently selected territory. I've edited my answer to show this idea.
Sep 10 '11 at 04:44 PM
aldonaletto
This is a great, exhaustive answer. If it helped you, be sure to award @aldonaletto an upvote by hitting the thumbs-up button and to mark the answer as correct. :) +1 from me.
Sep 12 '11 at 11:50 AM
CHPedersen
(comments are locked)
|
|
Thanks So much! that helped alot. of course, i still have one more problem. The Box and Information shows up below the mouse... I want it to show up in the same place every time. (right corner of the screen) how would I apply this? Gui Box instead of rectangle? or... how would I do that. Thanks. :) Just define the rect yourself in GUI.Box (use Screen.width to calculate the left coordinate): You can remove all the rect stuff from the other lines in both scripts.
Sep 12 '11 at 11:13 AM
aldonaletto
Thanks!!! It Works Now! although i still have one problem... my descriptions go out of the box.... is there a way to set a limit so that the automatically skip to the next line? Once again, Thanks!!!
Sep 12 '11 at 08:20 PM
sketchers1
You must set Word Wrap in the style variable at the Inspector - this will break the lines. Case the text is too big, it will overflow down the box. You can use a GUI function to calculate the height based on a given width and text:
var h = style.CalcHeight(GUIContent(information), 300);
GUI.Box(Rect(Screen.width-300,10,300,h), information, style);
I edited the answer to show these last modifications - including a w variable to set the box width.
Sep 14 '11 at 05:11 PM
aldonaletto
oh... i have come upon another problem. I have a lot of information typed in word.. I wanted to copy it and paste it into the Information option but it wont let me... is there a way to fix that?
Oct 07 '11 at 06:29 PM
sketchers1
(comments are locked)
|
