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 MithosAnnar · Apr 06, 2012 at 11:38 AM · movementtouchdirection

EARTH CALLING ANSWER!!!!

I am making a 2d game. I have an object, that I want to move by swiping. But I want to move in the direction of the swipe (not to the swipe).

I have a script but it doesn't work, because I don't know how, in javascript, to calculate the overall direction of a swipe and make my player move in that direction.

What is the best way to move parallel to a swipe's direction in 2D space?

Thanks for any answers!

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 fafase · Apr 06, 2012 at 11:46 AM 0
Share

Take the starting point and ending point of the swipe and make it a vector. Then you can use it to move your object.

To make the vector simply get the differences between each x,y and z and store them in a Vector3.

avatar image MithosAnnar · Apr 06, 2012 at 12:02 PM 0
Share

@fafase: Thanks for the suggestion, this we tried a while ago and for some reason didn't work.

Cheers for the help though!

1 Reply

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

Answer by MithosAnnar · Apr 06, 2012 at 12:01 PM

We solved it!

Here is the script for anyone who is interested!

 var myTransform : Transform;
 
 var mousePositionStart : Vector3;
 var mousePositionEnd : Vector3;
 
 var direction : Vector3;
 
 var move : boolean = false;
 
 var speed : float = 500;
 
 function Update () {
       
    myTransform.position.z = - 0.5;
          
    rigidbody.velocity = direction * Time.deltaTime * speed;
                
 if (Input.GetMouseButtonDown(0)) {
 
    mousePositionStart = Input.mousePosition;
          
 }
 
 if (Input.GetMouseButtonUp(0)) {
 
    mousePositionEnd = Input.mousePosition;
                
 if (mousePositionEnd.sqrMagnitude >= mousePositionStart.sqrMagnitude || 
     mousePositionEnd.sqrMagnitude <= mousePositionStart.sqrMagnitude) {
                    
    move = true;
                   
 }
 
 if (move == true) {
       
    direction = vector3(mousePositionStart, mousePositionEnd);                                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
    move = false;
 
 }
 
 }
 
 }
 
 function vector3 (from : Vector3, to : Vector3) {
    
    return (to - from).normalized;  
 
 }

Thank you, unity forum!!

http://forum.unity3d.com/threads/30632-Vector3.Direction

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

Touch movement not working 1 Answer

4 way direction movement using a Character Controller. 1 Answer

Character rotating to previous rotation 1 Answer

How do I keep my character facing the direction of travel after movement stops? 1 Answer

tranform.TransformDirection not effecting spawned object 2 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