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
1
Question by LordZephyr · Sep 23, 2012 at 09:36 PM · movementiosswipeforwardbackward

How do I add a forward and backward move by a swipe to my iOS script?

Hey, everyone,

I almost have it all! With help from you guys, I have my left and right swipe script working great but I want to move forward and backward on the up and down swipe instead of rotating. I don't need my players looking at the ceiling or the floor. This is what I have so far:

 #pragma strict
 
 private var h : float;
 private var v : float;
 var horozontalSpeed : float = 1.0;
 var verticalSpeed : float = 1.0;
 
 function Update()
 {
     if (Input.touchCount == 1)
     {
         var touch : Touch = Input.GetTouch(0);
 
         if (touch.phase == TouchPhase.Moved)
         {
             h = horozontalSpeed * touch.deltaPosition.x ;
             transform.Rotate( 0, -h, 0, Space.World );
 
             v = verticalSpeed * touch.deltaPosition.y ;
             transform.Rotate( v, 0, 0, Space.World );
         }
     }
 }

I want to change the "v = verticalSpeed * touch.deltaPosition.x; transform.Rotate( v, 0, 0, Space.World );" line from rotating the player to moving him forward and backward. So, when you swipe left and right, the player turns left and right but when you swipe up and down, the player moves forward and backward. I thought it was a deltaPosition.z but that didn't work for me when I tried it. I looked around but all I can find is information about rotation and not forward and backward movement.

Any help fixing my javascript would be greatly appreciated.

Thanks so much! Tom

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 Fattie · Sep 25, 2012 at 07:27 AM 1
Share

$$anonymous$$, conceivably this book-length answer could help you

http://answers.unity3d.com/questions/292333/how-to-calculate-swipe-speed-on-ios.html Cheers

avatar image AlucardJay · Sep 25, 2012 at 08:35 AM 0
Share

Thanks, that was one of the links I gave the OP on their last question ;)

http://answers.unity3d.com/questions/318631/how-do-i-javascript-swipe-to-move-my-first-person.html

I'm thinking of developing this answer into a more tutorial-type response. I did one for Invoke Repeating :D

http://answers.unity3d.com/questions/319562/i-need-help-with-a-block-spawner.html

1 Reply

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

Answer by AlucardJay · Sep 23, 2012 at 09:43 PM

Hi again, you have an input already for the Y-slide ( the variable v ), it is just how you use it. Movement is just a change in the transform.position. Replace the last line ( after v is calculated ) with :

 transform.position += transform.forward * v * Time.deltaTime;

Edit : here is my test script :

 #pragma strict
 
 public var horizontalSpeed : float = 1.0;
 public var verticalSpeed : float = 1.0;
 
 private var h : float = 0.0;
 private var v : float = 0.0;
 
 private var lastPos : Vector3 = Vector3.zero;
 
 function Update()
 {
 #if UNITY_EDITOR
     if ( Input.GetMouseButtonDown(0) )
     {
         lastPos = Input.mousePosition;
     }
     else if ( Input.GetMouseButton(0) )
     {
         var delta = Input.mousePosition - lastPos;
     
             h = horizontalSpeed * delta.x ;
             transform.Rotate( 0, -h, 0, Space.World );
 
             v = verticalSpeed * delta.y ;
             transform.position += transform.forward * v * Time.deltaTime;
         
         lastPos = Input.mousePosition;
     }
 #else
     if (Input.touchCount == 1)
     {
         var touch : Touch = Input.GetTouch(0);
 
         if (touch.phase == TouchPhase.Moved)
         {
             h = horizontalSpeed * touch.deltaPosition.x ;
             transform.Rotate( 0, -h, 0, Space.World );
 
             v = verticalSpeed * touch.deltaPosition.y ;
             transform.position += transform.forward * v * Time.deltaTime;
         }
     }
 #endif
 }
Comment
Add comment · Show 7 · 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 LordZephyr · Sep 23, 2012 at 10:23 PM 0
Share

Wow! Thanks, @alucardj. I was hoping you would answer my question. That's actually what I thought but when I tried to put in "transform.forward" it didn't work for me because I didn't change "transform.Rotate" to "transform.position".

Anyway, after I put in your line of code and tried it, my first person character jumped way off to the left and completely out of the scene. I zoomed out and found it and when I moved my finger around the screen, it had spasms. All I did was replace the last "transform.Rotate( v, 0, 0, Space.World );" with exactly what you typed. I even copied and pasted into my script. Do you know what I did wrong?

Thanks so much for your help. $$anonymous$$

avatar image AlucardJay · Sep 24, 2012 at 04:09 AM 0
Share

try

 transform.position = transform.forward * v * Time.deltaTime;

this should slow it down, if not then decrease verticalSpeed as well as changing to the above line.

I didn't think about how fast the object would move. The rotation takes a much larger number to rotate. Usually movement is always multiplied by Time.deltaTime so a reasonable value can be used for speed. Sorry I forgot this, I have edited the answer.

avatar image LordZephyr · Sep 24, 2012 at 10:01 PM 0
Share

I'm sorry, @alucardj. I just tried it and, although it doesn't "spasm" any longer, it still throws the player way off of my working area in to negative space. It won't move forward or backward. I can turn but not move. It's strange, too because the player doesn't fall. It just hangs there in space. I don't understand why it shoots way off once I touch the screen.

Sorry for the problems but I really appreciate your help. $$anonymous$$

avatar image AlucardJay · Sep 25, 2012 at 06:29 AM 0
Share

Not at all, I'm sorry for giving incorrect answers. I have been testing this today in the editor (I am unable to test on a touch device), and have found the problem to be my answer. Here is the correct answer ( it was missing a + ):

 transform.position += transform.forward * v * Time.deltaTime;

I have updated the answer. However, I think it should be pointed out that for different screen resolutions, the 'character' will have different speeds, and this is something that should be taken into consideration. Here is an example of that (scroll down to my answer, note the lines with Screen.width; and Screen.height;) : http://forum.unity3d.com/threads/142758-$$anonymous$$oving-objects-with-your-finger

I shall work on a 'character controller' like what you are making and submit it hopefully later tonight. Again, sorry for the mis-information.

(I also had to fix the spelling of horizontal as the variable name, on the other question it was the original posters spelling, I left it so as not to break their other code)

avatar image LordZephyr · Sep 26, 2012 at 12:23 AM 0
Share

Thank you so much, @alucardj! It works beautifully. And thank you @Fattie for your input. I found it to be very helpful.

I'm wondering if I could impose one more question to you, @alucardj that I don't think is necessary to post as a separate question. Where can I learn more about javascripting for Unity? I mean, I bought many books to help me learn Unity but from your answers, I would love to accumulate the amount of knowledge that you have... even if it takes me years. I just don't know where to go. The Docs are so sterile. They tell you what the functions and commands do but not the theory behind them or how to combine functions to get to do what you want. I am very interested in learning as much as I can. Thanks so much. FOR EVERYTHING!!!

$$anonymous$$

Show more comments

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

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

My player swipes faster on iOS then android 1 Answer

iOS Swipe while game is paused 0 Answers

Swipe to kill enemy? 2D 1 Answer

how do I sync movement with mouselook 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