- Home /
Touch Scrolling momentum
Hello everyone
I have been using a .js touch script for use with a sidescroller and it works lovely.
(Script Below)
But now I want to add momentum after the user lifts their finger... Any Ideas?
#pragma strict
// Moves object according to finger movement on the screen
var speed : float = 0.05;
var nonSpeed : float = 0;
function Update () {
if (Input.touchCount > 0 &&
Input.GetTouch(0).phase == TouchPhase.Moved) {
// Get movement of the finger since last frame
var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
// Move object across XY plane
transform.Translate (-touchDeltaPosition.x * speed,
0,0);
// -touchDeltaPosition.y * nonSpeed,0);
}
}
Thanks Y'all
~be
Anyone? Here is a script that uses the Left & Right Arrow $$anonymous$$eys (as well as touch) so you can test
#pragma strict
// $$anonymous$$oves object according to finger movement on the screen
var speed : float = 0.05;
var nonSpeed : float = 0;
function Update () {
// For arrow keys:
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow) ){
Debug.Log("right");
transform.Translate (Vector3(-1,0,0) * Time.deltaTime*speed);
}
if ( Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow) ){
Debug.Log("left");
transform.Translate (Vector3(1,0,0) * Time.deltaTime*speed);
}
// For Touch
if (Input.touchCount > 0 &&
Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) {
// Get movement of the finger since last frame
var touchDeltaPosition:Vector3 = Input.GetTouch(0).deltaPosition;
// $$anonymous$$ove object across X plane
transform.Translate (-touchDeltaPosition.x * speed,
0,0);
//transform.Translate (-touchDeltaPosition.z * speed,
//0,0);
//}
//}
Thanks!
I'd actually love to do this in C#
~Be
Answer by cruisereric · Jan 12, 2018 at 09:53 PM
Although it's been a little late for the answer to be given but for pple who had same problem (I know that the original question was in javascript but Im using c#, so excuse me), I've had the same problem and I end up adding rigid body to my object and then a Constant Force component to it. then In the rotation code, I called a CoRoutin in which I assign a value to Y part of relative torque of the constant force component and in some seconds I set it from 4 to 0.
IEnumerator slowDown(bool forward /*check if the movement is in the positive direction or negative direction*/){
//the initial value of relative torque's Y
speed = 4;
while (speed > 0) {
speed -= 0.5f;
//cf has been assigned in Start function to be the Constant Force component of the game object
cf.relativeTorque = new Vector3(0f,forward?speed:-speed,0f);
yield return new WaitForSecondsRealtime (1f);
}
// we make sure everything is 0 after the while loop has been finished
cf.relativeTorque = new Vector3(0f,0f,0f);
yield return new WaitForSecondsRealtime (0f);
}
It is working good in my case and you may need to add more logic or change default values to better tune it with your environment.
Your answer
Follow this Question
Related Questions
How to draw a line with touch? 1 Answer
Touch movement not working 1 Answer
EARTH CALLING ANSWER!!!! 1 Answer
Camera momentum after lifting finger 0 Answers
Inertia Scrolling Touch 1 Answer