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 Arishtanemi1998 · Sep 10, 2014 at 10:28 AM · positiontouchmovetranslate

move an object towards the touch or mouseclick position

hey. i have a cube which is going round and round along the a and y axes and i need it to translate it towards the place where the user touches or clicks the screen with a limited speed. the object must move in the direction as long as the finger or cursor is pressed. currently i an using a code which stores the location of the object and its change in position every second. here's the code

//Unity will constantly check the position of the GameObject. function Update () {

//Creates a variable to check the objects position.

myPosition = transform.position;

//Prints the position to the Console.

Debug.Log(myPosition); }

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

2 Replies

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

Answer by 767_2 · Sep 10, 2014 at 10:49 AM

 void Update()  
     {
         if(Input.GetMouseButtonDown(0))
         {
            newPosition.x = Input.mousePosition.x;
            newPosition.y = Input.mousePosition.y;
             //set new position as destination of your object
 
         }

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
1

Answer by NickP_2 · Sep 10, 2014 at 10:53 AM

In human language: - Get the world position of the mouse position (worldPos vs screenPos) - get the normalized direction, formula: direction = normalize(end position - currentposition), and multiply it by a certain speed. (always normalize, or else the speed ill be different each location

To get the position of the mouse, simple use:

 Vector3 mousePos = Camera.Main.ScreenpointToWorldPoint(Input.MousePosition);
 //Because the object's Z position has to be the same:
 mousePos.z = transform.position.z;

Get normalized direction:

 Vector3 dir = (mousePos - transform.position).normalized;
 //Create final force where speed is a float
 Vector3 force = dir * speed;

To noly execute this code when the mouse button is down, use:

 if(Input.GetMouseButton(0))//0 is left, 1 is right
 {
     //....
 }



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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Boat moving forward while rotating towards point 1 Answer

translate 2 Answers

Loop from Point A to Point B after Point C 2 Answers

touch move slide object 0 Answers

iOS -Touch position unchanged, deltaPosition reported -1 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