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
0
Question by zollil · Apr 30, 2015 at 10:40 PM · augmented-reality

Move 3d object to the position of mouse click

I have an AR scene that has one AR camera, an image target and a 3D object as below. alt text

I want to move the AR object to the position of the mouse click. I have tried taking the Input.mouseposition (screen position), converting it using ScreenToWorldPosition and moving the 3D object to this position. The object is moved, but not to the mouse click position.

How can I move the object to the position of the mouse click? My code is as follows:

 Camera cam;
 Vector3 target = new Vector3(0.0f, 10f,0.5f);
 // Use this for initialization
 void Start () {
 if (cam == null)
     cam = Camera.main;
 }
 void Update()
 {
     if (Input.GetMouseButtonDown(0)) {
         Debug.Log("MouseDown");
 
         Vector3 mousePos = Input.mousePosition;
         mousePos = cam.ScreenToWorldPoint(mousePos);
         GameObject.Find("Car1").gameObject.transform.position = mousePos;
     }                                                                
 }

To move 3d object with mouse, I tried this code. But it don't move the position of mouse click. link text

1.jpg (20.6 kB)
Comment
Add comment · Show 2
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 0potable · May 06, 2015 at 03:04 PM 0
Share

Try this maybe : https://gist.github.com/Opotable/3cb4937a693bd7cc8ec5

avatar image Fappp · May 06, 2015 at 06:24 PM 0
Share

Is the object supposed to move on a flat or non-flat surface? And what camera angle are you using?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by siaran · Apr 30, 2015 at 11:04 PM

First, you may want to try unparenting your Car1 object from ImageTarget, see if that makes a difference.

Also, you may want to try shooting a ray from your mouse position and setting your object position to your ray's hit point. like

 GameObject car;
 
 void Start(){
  car = GameObject.Find("Car1");
 }
 
 void Update(){
  if(Input.GetMouseButtonDown(0)){
   RaycastHit hit;
    if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit){
     car.transform.position = hit.point;
   
   }
  }
 }

Dunno if I got all the {} things correct there, but you get the idea.

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 zollil · May 06, 2015 at 12:49 PM 0
Share

I tried it. $$anonymous$$y scene has only an AR camera and a car object. I created a C# file and attached to AR camera. I used your code. But the object wasn't moved mouse click position.

avatar image
0

Answer by 0potable · May 07, 2015 at 09:15 AM

Try this : http://pastebin.com/GSSMnE9R

Build up this very quickly. Post it once but as a comment (derp).

  1. Save the file onto your Assets folder inside your project

  2. Inside your if(Input.GetMouseButtonDown(0)), just call :

    Vector3 resultWorldPosition = MathfLib.MousePointToWorldPoint(yourCamera, theDistanceFromThisCamera);

Should do the trick ;)

Tell me if you want a version with raycast.

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

6 People are following this question.

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

Related Questions

Cloud recognition in Vuforia 0 Answers

Very confused at what I should do 0 Answers

AR camera not working in app test 0 Answers

Returning to a scene with AssetBundle 1 Answer

Vuforia Multi Object Collision 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