- Home /
How to go about this?
Im creating an interior scene whereby users can click on objects to change color or textures.
The textuers portion is solved. but the problem start here.
i want to create something goes like this:-
awake function is hide cursor
1.right click to show cursor
2.mouseover to object, object is highlighted, gui options appear, also at the same time the screen is freezed
3.left click to select options (but how to create different options for different objects?)
4.after left click, user can go back to walk around..
Can anyone tell me how to go about with that? preferably to use only javasccript.
thanks
Do you develop for iPhone, web or standalone? Because there are some differences on iPhone and web.
Answer by Bunny83 · Jan 13, 2011 at 04:03 AM
The language doesn't matter ;) i prefer C# but nearly everything can be done in both languages.
Input.GetMouseButtonDown(1) is what you need to detect the right mouse button (0==left; 1==right 2== middle). To lock or unlock the cursor use Screen.lockCursor
3DObjects don't have events like mouseover, but you can use Physics.Raycast () in combination with Camera.main.ScreenPointToRay (Input.mousePosition); to get the object you're pointing at. I guess you use the FPSWalker script (or FPSController or how it's names now ;) ). To prevent moving you have to disable the script. Use GetComponent() to access your script and disable it.
Here you have to be a bit more precice what kind of GUI you need, how many different objects/GUIs you have/need.
Inside one of your GUI.Button() just repeat what i mentioned in 2. Use GetComponent() to access your script and enable it. And lock the cursor again.
*Edit*14.01@03:29
Well if you want a complete different GUI for each object i would use a strategy pattern to offer a common interface. That's quite advanced stuff for a beginner but a basic "tool" in oop. ;) I'm not that deep into Javascript so it's difficult to show you an example in JS but i try:
That's a simple "base class". Every Object have to be derived from this class in order to be able to get selected:
class SelectableObject extends MonoBehaviour
{
// static var to hold the current selected object
public static var m_CurrentSelected : SelectableObject = null;
// define common vars or functions here
public var Health : float = 100.0f;
}
Here is an example of one of your object scripts:
class Object1 extends SelectableObject
{
function Update (){
//Do normal stuff related to your object
}
function OnGUI(){
// check if we are selected, if not, return
if (m_CurrentSelected != this)
return;
// Draw Your GUI for this object here
if (GUILayout.Button("deselect")) {
SelectableObject.m_CurrentSelected = null;
}
}
}
with this little "trick" you can select an object by it's script component.
Just assign the object you hit with your ray to SelectableObject.m_CurrentSelected
That function is your "picker" script and should be attached to maincamera
function Update() {
if (Input.GetMouseButtonDown(1)) {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
var obj = hit.collider.GetComponent(SelectableObject);
if (obj != null) {
SelectableObject.m_CurrentSelected = obj;
}
}
}
}
One last important hint: the script filename and the class name have to be the same. In C# you always have to do that, in JS you don't need to define the class but in that case we want a different base class (in Unity classes have to be derived from MonoBehaviour to be able to attach it to a GameObject)
Good luck! It's not easy stuff for beginners, but everyone have to start somewhere ;)
Hi Bunny, thanks for the tips, for no.3, i trying to create like mouse over to wall for an example i want the guicontent to fade in just beside the cursor, then freeze screen in order for the user to click on the selection.
& also i have yet to find an answer on how to script to create differnt materials for differnt objects... care to tell me more?
I will edit my post tomorrow, need some sleep ;) I'v started with some example scripts in JS, but it's quite advanced stuff...
well i just wanted to show you some hints, but i ended up in testing it myself ;)
"3DObjects don't have events like mouseover" actually they do, just add a collider and use On$$anonymous$$ouseEnter/Over/Exit.
Answer by _Petroz · Jan 14, 2011 at 05:03 AM
It needn't be quite as complex as the other answer, here is some pseudocode to get you started:
awake function is hide cursor
Screen.lockCursor = true;
1.right click to show cursor
Screen.lockCursor = false;
2.mouseover to object, object is highlighted, gui options appear, also at the same time the screen is freezed
some variable such as:
var showGUI : boolean = false;
inside OnMouseEnter() change to highlight material, showGUI = true; Time.timeScale = 0;
inside OnMouseExit() change to default material, showGUI = false; Time.timeScale = 1;
function OnGUI()
{
if(showGUI)
{
// display gui
}
}
3.left click to select options (but how to create different options for different objects?)
This kind of polymorphic behaviour is non-trivial. The other answer touches on this stuff.
4.after left click, user can go back to walk around..
well didn't realized that On$$anonymous$$ouseEnter is also called for colliders ;) that's really great. So Unity is doing a raycast each frame by default (except on iPhone) and trigger these events. Thanks for the hint.
thanks guys... will try out soon, i will let u guys know in a while
Your answer
Follow this Question
Related Questions
Detect cursor on edge of screen 1 Answer
Is it possible to lock a cursor without hiding it?? 0 Answers
Show/Hide Mouse on Screen Position 1 Answer
Lock cursor everywhere not center,how? 1 Answer
On Mouse Over Problem 1 Answer