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 reptilebeats · Jul 31, 2012 at 03:08 AM · positiontouch

move objects by touch position

basically i have 5 objects all parented to a root object, and when i click on one of the children it moves the parent by the touch position on the screen, but what i can't work out is how to move the parent from the current position it is at, the parent will always move back to the touch position.

so i need to move the object + the touch position but not so that it keeps adding movement

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 reptilebeats · Jul 31, 2012 at 03:33 AM 0
Share

To make it simpler i want ti move object a position.x + touch position.x. But not to keep adding the touch position to the object.

At the moment its in an update so it keeps moving by touch position when i only want to move it by the how much the touch position has moved. So if touch moves by 2 the object a will move by two not two every frame.

Its got to happen whilst my finger is moving though not after

avatar image flamy · Jul 31, 2012 at 04:23 AM 0
Share

hmm it would be easy to say if you have posted some code snippet you are using!

avatar image reptilebeats · Jul 31, 2012 at 10:43 AM 0
Share

Gameobject.find("OBjectA").transform.position.x = touch.position.x

If i use plus it will keep adding every frame i just need it to he added once

1 Reply

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

Answer by flamy · Jul 31, 2012 at 11:22 AM

Im still not clear about what exactly you want to do but i guess you should be using Events from unity..

I guess you want to do something like the below snippet

     void OnGUI()
  {
  if(Event.current.type == EventType.mouseDrag)
  {
  GameObject.find("OBjectA").transform.position +=(Vector3)Event.current.delta;
  }
  }

delta variable from Event will return the value change from last frame (when mouse drag is happening). I guess that is what you are trying to do?!! but even then that is not a proper way to do. since touch.Position or Event.current.delta or Input.mousePosition, all these will be in screen co-ordinate, you should be converting it to world co-ordinate value, which would take considerable amount of work.

I think the best would be converting the touch or mouse position to world coordinate. and add an offset to it depending upon the difference between the object position and touch down position. The below is jus a sample i have checked. I hope i might give you some idea to continue further.

     Vector3 offset;
  Transform _object;
  
  void Start () {
  _object=GameObject.Find("OBjectA").transform;
  }
  
  void OnGUI()
  {

    if(Event.current.type == EventType.mouseDown)
    {
    offset = _object.position- Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, _object.position.z-Camera.main.transform.position.z));
    }
  
     if(Event.current.type == EventType.mouseDrag)
    {
     _object.position = Camera.main.ScreenToWorldPoint(new  Vector3(Input.mousePosition.x,Input.mousePosition.y, _object.position.z- Camera.main.transform.position.z)) + offset;
    }
  }
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 reptilebeats · Jul 31, 2012 at 08:05 PM 0
Share

got it i will give it a go, sorry i wasn't at my computer to give a proper script snippet

avatar image reptilebeats · Jul 31, 2012 at 08:10 PM 0
Share

here is a better snippet of code, I'm working in java, but i've been $$anonymous$$ching myself cSharp as well so its fine to answer in cSharp.

var touchPos : Vector3;

touchPos = Camera.mainCamera.ScreenToWorldPoint(touch.position);

               if(touch.phase == TouchPhase.$$anonymous$$oved){
                   object.collider.enabled = true;
                       moveObjects.transform.position.x = touchPos.x;

avatar image reptilebeats · Jul 31, 2012 at 08:12 PM 0
Share

its just the bottom line, oh and its inside an update function, so it keeps adding the touch position if i use + every frame.

sorry I'm terrible at explaining

avatar image reptilebeats · Aug 01, 2012 at 01:14 AM 0
Share

ok yh this is what i was looking for, works fine, the object will move anywhere now without constantly adding the amount of touch position, to it. its just 5 added to its current position not 5 every dam frame.

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

7 People are following this question.

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

Related Questions

Touch position off from real position? 1 Answer

solution to detect multi- finger touch position 2 Answers

How to reach limit line, keep moving, even past over the touch position? 0 Answers

Moving the camera to a touch position 2 Answers

Moving a GUI Image to the same position as a touch 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