Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Raymi95 · Apr 13, 2011 at 04:55 PM · javascriptobject

How move objects closer to camera when i mouse over it ??

I have seen a tutorial when one person made a 3d menu and when you select it it moves closer to the camera(it pulls out). I made it but the tutorial shows only when i do it with keyboard buttons (up and down). I want to do it with the mouse(when i mouse over it). How you see in MenuManager there is if(Input.GetAxisRaw("vertical"). Is there a way to do it with mouse ?? I mean i can pull out selected objects with up and down button of the keyboard. I want to pull out objects when i mouse over it. Can you pls help ?

Pls help

I will show you the java script:

menuItem

function OnSelected(on:boolean)
{
         //I just got selected
         if(on)
         {
                 iTween.MoveTo(gameObject,{"z":-5, "time":0.5});
         }
         else //I just got deselected
         {
                 iTween.MoveTo(gameObject,{"z":-2, "time":0.5});
         }
}

MenuManager

var menuItems:menuItem[]; // list of MenuItem(s)

var currentMenuItem:int = 0;

var keyDelay:float = 0.25;

function Start() { menuItems[currentMenuItem].OnSelected(true);

      while(true)
      {
              if(Input.GetAxisRaw("Vertical") > 0.9)
              {
                      menuItems[currentMenuItem].OnSelected(false);

                      currentMenuItem--;
                      if(currentMenuItem < 0) currentMenuItem = 0;

                      menuItems[currentMenuItem].OnSelected(true);

                      yield new WaitForSeconds(keyDelay);
              }
              else if(Input.GetAxisRaw("Vertical") < -0.9)
              {
                      menuItems[currentMenuItem].OnSelected(false);

                      currentMenuItem++;
                      if(currentMenuItem >= menuItems.length) currentMenuItem = menuItems.length - 1;

                      menuItems[currentMenuItem].OnSelected(true);

                      yield new WaitForSeconds(keyDelay);
              }       
              yield; 
      }       

}

Comment
Add comment · Show 3
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 Kourosh · Apr 13, 2011 at 05:14 PM 0
Share

Do you also want the object returns to its last position after its deselected/mouseOut?

avatar image Kourosh · Apr 13, 2011 at 05:15 PM 0
Share

And also by selecting you mean clicked or hover?

avatar image Raymi95 · Apr 13, 2011 at 06:20 PM 0
Share

yes i want the object to return when i mouseout. By selecting i mean mouse over it.

1 Reply

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

Answer by Kourosh · Apr 13, 2011 at 08:06 PM

Here's something basic for implementing what you have in mind. However you might wanna work on it to make nicer movements.

create buncha cubes and apply this script to you camera.

var speed:float = 0.07;

private var originalPosition:Vector3; private var hoverObject:Transform; private var hover:boolean = false; private var destination:Vector3;

function Update () {

 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 var hit : RaycastHit;
 if (Physics.Raycast (ray, hit, 100)) {
     if(!hover){
     hoverObject = hit.transform;
     originalPosition  = hoverObject.transform.position;
     hover = true;
     }
 }  else {
     hover = false;
 }

 if(hover){
     var p : Vector3 = camera.ScreenToWorldPoint (Vector3 (Input.mousePosition.x,Input.mousePosition.y,camera.nearClipPlane));
     destination = p;
 } else {
     destination = originalPosition;
 }
 if(hoverObject != null)
         hoverObject.transform.position = Vector3.MoveTowards(hoverObject.transform.position,destination,speed); // Here is the line where you would swap it with iTween functions using destination variable.

}

Good luck!

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 Raymi95 · Apr 13, 2011 at 08:23 PM 0
Share

God thank you i love you dude. That's what i was looking for. Now i must play with it to make the cubes stop when they are close enough. Thank you again

avatar image Kourosh · Apr 13, 2011 at 08:31 PM 0
Share

change "camera.nearClipPlane" to any z value you want to control how close they should get. I'm glad it helped.

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

Setting a variable to an instantiated object. 1 Answer

touch 3d object open gui 1 Answer

Drawing a 3D object javascipt 0 Answers

Disable GameObject Only Father Not Children 2 Answers

Rotating an Object on Key Press! 3 Answers


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