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 hollym16 · Aug 08, 2013 at 03:35 PM · raycastbuttonmenumouseclick

Button click moves GameObject to specific position

Hi there,

I'm making a menu in 3D and need a hand with it.

I've got several working buttons/tabs that has a rectangle above the current tab. I want to animate the rectangle to move to the relevant position on a mouse click. For example, it's currently above 'Welcome' but when 'Help' is clicked, the rectangle moves to above the 'Help' button.

I'm fairly new to coding so I know I need to write a script that says something along the lines of 'find out current position and move to this specific position on mouse click' (possibly using Raycast) but not sure how to phrase in it .js. I would appreciate any help!

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 hollym16 · Aug 08, 2013 at 03:03 PM 0
Share

Here's the script I've tried but whenever the button is clicked it goes 0.4 on the z axis

pragma strict

var transform : Transform; var NavBar : GameObject; private var ray: Ray; private var hit : RaycastHit; function Start () {

 }
 
 function Update () {
 
 if(Input.Get$$anonymous$$ouseButtonDown(0)){
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast(ray, hit)){
             if(hit.transform.name == "IndustryBtn"){
       NavBar.transform.Translate(0,0,0.4);

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jamora · Aug 08, 2013 at 03:40 PM

You are currently moving it to position (0,0,0.4). To move it to the position of the hit point, you will need

 NavBar.transform.Translate(hit.position);
Comment
Add comment · Show 1 · 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 hollym16 · Aug 08, 2013 at 03:46 PM 0
Share

Thanks for the reply.

That seems to make more sense, however, do I need to add a var because I'm getting an error message saying "'position' is not a member of 'UnityEngine.RaycastHit'.

avatar image
0

Answer by hollym16 · Aug 09, 2013 at 09:27 AM

Thanks to Jamora (and some research) I've worked it out! That last bit needs to be:

 NavBar.transform.position = hit.point;

I just need to work out how to constrain it to one axis now, anyone have any ideas?

Comment
Add comment · 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
0

Answer by falconer · Oct 22, 2014 at 05:53 AM

This is pretty simple. You can use the Vector3.Lerp function to achieve this. Use raycasting to get the mouse click position or the touch position. Then use the initial and the final position in the lerp function. The initial position being the position that the gameobject is at now and the final position being the click / touch position. You can find the article by The Game Contriver on the same here

Move to Touch / Click Position - The Game Contriver

Comment
Add comment · 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

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Inspector scripting, adding lists, checkboxes and buttons [With Image] 0 Answers

How to do Menu and Submenu UI - Hide/Collapse 2 Answers

Main Menu button in pause menu 0 Answers

Back button on menu to go back to previous menu screen 0 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