# How to make moving character like in Feeding Frenzy?

This game I create that the characters is moving to right only. But I want like Feeding Frenzy, when I moving the mouse the characters will follow the cursors and when it press short click the character have more speed.

The problem in my code is when I press long left click, the character also have more speed

this is my code:

``````//variable for moving characters
public float moveSpeed;
public float turnSpeed;
private Vector3 moveDirection;

//variable for detect short/long click
private float t0;
private bool longClick;
private bool shortClick;

void Start ()
{
moveDirection = Vector3.right;
t0 = 0f;
longClick = false;
shortClick = false;

}

void Update ()
{
// code to moving my characters (moving to right only) & following cursor
Vector3 currentPosition = transform.position;
Vector3 moveToward = Camera.main.ScreenToWorldPoint( Input.mousePosition );
moveDirection = moveToward - currentPosition;
moveDirection.z = 0;
moveDirection.Normalize();
if (moveDirection.x <= 0)
{
moveDirection = Vector3.right;
}

Quaternion rot = Quaternion.LookRotation (transform.position - moveToward, Vector3.forward);
rot *= Quaternion.Euler (0, 0, 90);
transform.rotation = rot;
transform.eulerAngles = new Vector3 (0, 0, transform.eulerAngles.z);
Vector3 target = moveDirection * moveSpeed + currentPosition;
transform.position = Vector3.Lerp( currentPosition, target, Time.deltaTime );
float targetAngle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Slerp( transform.rotation,
Quaternion.Euler( 0, 0, targetAngle ),
turnSpeed * Time.deltaTime );

//==================THIS IS MY PROBLEM!========================
//IN MY CODE BELOW, WHEN I PRESS LONG CLICK IT ALSO have moveSpeed=12.
//I WANT moveSpeed=12 WHEN I PRESS SHORT CLICK ONLY
{
t0 = Time.time;
moveSpeed = 12;
}

else if (Input.GetMouseButtonUp(0) && (Time.time - t0) > 0.5f)
{
longClick = true;
moveSpeed = 3;
}
else if (Input.GetMouseButtonUp(0) && (Time.time - t0) < 0.5f)
{
shortClick = true;
moveSpeed = 3;
}
longClick = false;
shortClick = false;
//=======================================================
}
``````

you want to mix in mousebutton with buttonup and buttondown

what were going to do is place code inside update that is a timer and when you press down it’ll start the timer, when you pass a thresh hold it will increase speed to a number.
if you lift up before that it’ll increase it to a lesser amount

the key is knowing that down gets when it go down
up when the mouse goes up

and get mouse button lets up know when its being held down.

get button down only registers once on the single down. thats good for say single firing a gun.

get button lets us know when its held for say a machien gun firing.

here you go roughly

``````float timer;
bool DoBigBurst;
input.getmousebuttondown(0)
{
//start the timer by resetting it
timer = 0;
DoBigBurst = false;
....
}

input.getmousebutton(0)
{
timer += time.deltatime //every frame add to the timer the time between now and the last frame
if(timer > ThresholdTime)
DoBigBurst = true;

if(DoBigBurst)
movespeed = longspeed; // use a variable not say 12 because you might want to change it later
}

if (input.getmousebuttonup(0)
{
if(!DoBigBurst)
movespeed = shortspeed // they lifted up the mouse before the long burst time, so just a short one
else
//probably do nothing, they already got a big burst of speed so you might just ignore this part
}
``````