- Home /
Decrease a value
hi...its urgent...now i am doing 2d side scrolling game ...when user swipe a screen..a tyre(game hero) will move forward when i remove a touch from screen..it should slow down.....agin when i swipe it should gain a speed.....this is my script used so far...here gaining speed by swipe works good..but loss a speed makes me annoying and gaining...please help me.......
void Update()
{
//move upward
velocity = _controller.velocity;
if( _controller.isGrounded )
velocity.y = 0;
if (Input.touchCount > 0 )
{
Touch touch = Input.touches[0];
if(touch.phase==TouchPhase.Began &&jumpBtnOK==false)
{
Debug.Log("touch");
startPos = touch.position;
}
if((touch.phase == TouchPhase.Began && jump_btn.guiTexture.HitTest(touch.position) ) && _controller.isGrounded && jumpBtnOK)
{
Debug.Log("jump_btn");
animation.Play("jumpButton_anim");
var targetJumpHeight = 1f;
velocity.y = Mathf.Sqrt( 2f * targetJumpHeight * -gravity );
if(windZone)
{
velocity.y = Mathf.Sqrt( targetJumpHeight * -gravity );
}
}
if(touch.phase==TouchPhase.Moved )
{
float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
// direction = Input.touches[0].deltaPosition.normalized; //Unit Vector of change in position // speed = Input.touches[0].deltaPosition.magnitude / Input.touches[0].deltaTime; swipeToStart.active = false; anim.SetTrigger("move");
if(touchDetection==true )
{
if (swipeDistVertical > minSwipeDistY)
{
float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
// if (swipeValue > 0 && _controller.isGrounded )//up swipe // { // Debug.Log("up"); // var targetJumpHeight = 1f; // velocity.y = Mathf.Sqrt( 4f targetJumpHeight -gravity ); // // if(windZone) // { // velocity.y = Mathf.Sqrt( targetJumpHeight * -gravity ); // } // //Jump (); // }
}
float swipeDistHorizontal = (new Vector3(touch.position.x,0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
if (swipeDistHorizontal > minSwipeDistX)
{
float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
if (swipeValue > 0 && _controller.isGrounded&&ended==false)//right swipe
{
if(windZone==false)
{
Debug.Log("right");
jumpBtnOK=true;
runSpeed+=0.5f;
}
if(windZone==true)
{
Debug.Log("windzone come");
runSpeed+=0.1f;
if(runSpeed<0)
{
runSpeed=0;
}
}
normalizedHorizontalSpeed = 1;
anim.SetTrigger("move");
float speedAnimTyre;
anim.SetFloat("move",runSpeed);
anim.SetFloat("speed",runSpeed);
//animation["tyre_anim_2"].speed= runSpeed;
if( transform.localScale.x < 0f )
transform.localScale = new Vector3( -transform.localScale.x, transform.localScale.y, transform.localScale.z );
}
}
}
}
if((touch.phase==TouchPhase.Ended || touch.phase==TouchPhase.Canceled))
{
Debug.Log("Ended");
ended=true;
if(windZone==false && ended==true)
{
Debug.Log("ended cancel");
//slowTyre();
StartCoroutine(slowDown(5.0F));
ended=false;
}
}
}
var smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping; // how fast do we change direction?
velocity.x = Mathf.Lerp( velocity.x, normalizedHorizontalSpeed * rawMovementDirection * runSpeed, Time.deltaTime * smoothedMovementFactor );
//Debug.Log("runtime"+runSpeed);
// apply gravity before moving
velocity.y += gravity * Time.deltaTime;
_controller.move( velocity * Time.deltaTime);
}
void slowTyre()
{
Debug.Log("slowtyre");
StartCoroutine(slowDown(5.0F));
if(runSpeed<0)
{
runSpeed=0.5f;
}
ended=false;
}
IEnumerator slowDown(float waitTime=1.0F)
{
Debug.Log("inenymartoe");
yield return new WaitForSeconds(waitTime);
if(runSpeed>0)
{
runSpeed-=0.5f;
slowTyre();
}
}
So what is now happening when you enter the slowing down mode ? And what should happen ins$$anonymous$$d ?
Your answer
![](https://koobas.hobune.stream/wayback/20220613153705im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can i use single swipe to run a character continuously. 0 Answers
Swipe Gesture iOS 1 Answer
Controlling Animation ON TOUCH 0 Answers
Detect if finger lifted off screen 1 Answer
Multitouch GameObject slicing 0 Answers