- Home /
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
$$anonymous$$, conceivably this book-length answer could help you
http://answers.unity3d.com/questions/292333/how-to-calculate-swipe-speed-on-ios.html Cheers
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
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
}
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$$
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.
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$$
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)
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$$
Your answer
Follow this Question
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