Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 SunilUL · May 25, 2018 at 10:15 AM · androidmovementmobiletouchinteraction

How to touch, move and rotate 3D objects in XZ?

Hi, I'm new to coding and just started creating some POC.\

I'm new to coding and been searching for assets/ tutorials past 2 days and completely stuck in moving forward.

Can anyone please help me write a Code to Touch, Drag, Move and Rotate the 3D objects?

move: The 3D object should move only in X, Z position keeping the Y position locked.

rotate: It's great if the user can rotate the object when 2 fingers are swiped on the object. Or, it is even greater if when the user rotates the world space UI button( placed below the 3D object).

For testing, it's great if the code works for desktop too.

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 SunilUL · May 29, 2018 at 05:28 AM 0
Share

Is anyone available to help? :(

avatar image QKlon · May 29, 2018 at 05:44 AM 0
Share

That's such a general question that the only answer can be: RTF$$anonymous$$.

You need a collider on a 3D object to make it sensitive for collisions (even with the mouse / raycast). Read about colliders, Physics to do raycasts and the Input system. For positioning and rotating read the manual about the Transform component.

Learn your lesson, write code and come back with a particular problem providing your code so far that is not working as expected. I'm sure then you will get help.

avatar image SunilUL QKlon · May 29, 2018 at 09:10 AM 0
Share

Here is the code. This moves in XY position. and I'm unable to move it in XZ.

do you help here?

And, I know and have already tried as you told. If you can help solve this, you can. btw, I'm being polite.

 private Vector3 screenPoint;
     private Vector3 offset;
 
     
     void On$$anonymous$$ouseDown()
     {
         Debug.Log("mouse down");
         Vector3 pos = new Vector3 (transform.position.x, 0, transform.position.z);
         screenPoint = Camera.main.ScreenToWorldPoint(transform.position);
         offset = transform.localPosition - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
         Debug.Log (offset);
     }
 
     void On$$anonymous$$ouseDrag()
     {
         Debug.Log("mouse drag");
         Vector3 curPosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
         transform.localPosition = Camera.main.ScreenToWorldPoint (curPosition) + offset;
         Debug.Log (Camera.main.ScreenToWorldPoint (curPosition) + offset);
     }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by anthot4 · May 29, 2018 at 09:15 AM

Look at Input.GetTouch in the unity documentation.

Drag: You need to draw a raycast from the camera through the users touch position and check if that raycast hits the collider on the gameobject you want to move/drag/rotate. Then code it to check if the user moves their touch position the raycast it hitting a gameobject, if it is then move the gameobject to the position of the users touch position. If the user lifts their finger off the screen stop this process.

Rotate: It depends if you need the angle in radians or degrees for the ui or another part of your game for the best way to do this. Look at transform.eulerangles, quaternion,lookrotation and quaternion.slerp.

Comment
Add comment · Show 3 · 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 SunilUL · May 29, 2018 at 09:19 AM 0
Share

Hi, Thanks for the reply.

I've covered the basics :) void On$$anonymous$$ouseDown() { Debug.Log("mouse down"); Vector3 pos = new Vector3 (transform.position.x, 0, transform.position.z); screenPoint = Camera.main.ScreenToWorldPoint(transform.position); offset = transform.localPosition - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); Debug.Log (offset); }

      void On$$anonymous$$ouseDrag()
      {
          Debug.Log("mouse drag");
          Vector3 curPosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
          transform.localPosition = Camera.main.ScreenToWorldPoint (curPosition) + offset;
          Debug.Log (Camera.main.ScreenToWorldPoint (curPosition) + offset);
      }



Here is the code. This moves the object in XY Position., but I'm unable to solve it to move in XZ positions. I'm really new to coding, any help to make it move in XZ would be awesome.

avatar image anthot4 SunilUL · May 29, 2018 at 09:26 AM 0
Share

Where you have "Input.mousePosition.y" change that to "0" and I don't know what value your offset is but make that have a y value of 0 too. Also, I have read its better to raycast in the update function rather than using the "On$$anonymous$$ouse" functions as these are imprecise and don't give you as much control as raycasting.

avatar image SunilUL anthot4 · May 29, 2018 at 09:31 AM 0
Share

Sorry, this is the code..

The offset is to check the mouse position and drag it from there..

 void On$$anonymous$$ouseDown()
     {
         Debug.Log("mouse down");
         Vector3 pos = new Vector3 (transform.position.x, 0, transform.position.z);
         screenPoint = Camera.main.ScreenToWorldPoint(transform.position);
         offset = transform.localPosition - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
         Debug.Log (offset);
     }
 
     void On$$anonymous$$ouseDrag()
     {
         Debug.Log("mouse drag");
         Vector3 curPosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
         transform.localPosition = Camera.main.ScreenToWorldPoint (curPosition) + 0;
         Debug.Log (Camera.main.ScreenToWorldPoint (curPosition) + offset);
     }


and, i tried making them 0 as you told, the touched object now moves only in X

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

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

How to Move the Car With Touch? 1 Answer

Why this simple code doesnt work? 0 Answers

How to differentiate touches on mobile devices 1 Answer

Jump when touch the screen Android (GAME MADE FOR PC) 5 Answers

Do UI Buttons work the same for Touch for an Android Game? 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