Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Gary 2 · Jan 13, 2011 at 03:01 AM · screenfreezecursormouseoveroptions

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jan 14, 2011 at 08:23 PM 0
Share

Do you develop for iPhone, web or standalone? Because there are some differences on iPhone and web.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

  1. 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

  2. 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.

  3. Here you have to be a bit more precice what kind of GUI you need, how many different objects/GUIs you have/need.

  4. 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 ;)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gary 2 · Jan 13, 2011 at 05:22 AM 0
Share

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?

avatar image Bunny83 · Jan 13, 2011 at 07:51 AM 1
Share

I will edit my post tomorrow, need some sleep ;) I'v started with some example scripts in JS, but it's quite advanced stuff...

avatar image Bunny83 · Jan 14, 2011 at 02:38 AM 1
Share

well i just wanted to show you some hints, but i ended up in testing it myself ;)

avatar image _Petroz · Jan 14, 2011 at 05:03 AM 0
Share

"3DObjects don't have events like mouseover" actually they do, just add a collider and use On$$anonymous$$ouseEnter/Over/Exit.

avatar image
1

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..

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jan 14, 2011 at 08:15 PM 0
Share

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.

avatar image Gary 2 · Jan 19, 2011 at 10:22 AM 0
Share

thanks guys... will try out soon, i will let u guys know in a while

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Screen.lockCursor will not work 1 Answer

screen.lockcursor locks the cursor in wrong place 1 Answer

How to freeze screen to enable menu interaction? 1 Answer

How to make something happen when mouse on side of screen? 1 Answer

One Last GUI Question 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges