- Home /
Question by
zeeshandar · Sep 17, 2014 at 12:24 PM ·
c#movementtap
Long tap on the screen
i want to implement the long tap on the screen..If the user long taps on the screen the position of the x and y axis decreases and as soon as he releases the tap the x increases and the y decreases> i have messed around with somethings but no luck..heres the code i have been trying.
public class move : MonoBehaviour {
public Vector2 velocity = new Vector2(40,40);
public float forwardspeed=0.02f;
Vector2 movement;
// Use this for initialization
void Start () {
Debug.Log("start+"+Input.touchCount);
movement.x+=(forwardspeed);
movement.y-=(forwardspeed);
rigidbody2D.velocity = movement;
}
// Update is called once per frame
void FixedUpdate () {
int i=0;
while (i < Input.touchCount)
{
// Is this the beginning phase of the touch?
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
Debug.Log("input touch count"+Input.touchCount);
// rigidbody2D.gravityScale =0;
movement.x+=(forwardspeed);
movement.y+=(forwardspeed);
rigidbody2D.velocity = movement;
}
else if (Input.GetTouch(i).phase == TouchPhase.Ended)
{
movement.x+=(forwardspeed);
movement.y-=(forwardspeed);
rigidbody2D.velocity = movement;
}
++i;
}
}
}
Comment
Answer by VincentDB · Sep 17, 2014 at 12:49 PM
Didn't tried but should work:
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
startTime = Time.time;
}
if(Input.touchCount > 0 && startTime - Time.time > timer)
{
//Long tap stuff
longTap = true;
}
if (Input.GetTouch(i).phase == TouchPhase.Ended && longTap)
{
longTap = false;
//Long tap end stuff
}
Answer by Dhiru · Sep 17, 2014 at 01:04 PM
Here is a simple script. I think this should work fine.
private float forwardspeed;
void Start () {
forwardspeed = 1.5f;
}
void FixedUpdate () {
if (Input.GetMouseButtonDown (0)) {
mouseClicked = true;
}
if (Input.GetMouseButtonUp (0)) {
mouseClicked = false;
}
if (mouseClicked) {
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x + Time.deltaTime * forwardspeed, rigidbody2D.velocity.y + Time.deltaTime * forwardspeed);
}
else{
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x + Time.deltaTime * forwardspeed, rigidbody2D.velocity.y - Time.deltaTime * forwardspeed);
}
}
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can I make an object in my scene jump continuously? 1 Answer