- Home /
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;
//=======================================================
}
Answer by sparkzbarca · Apr 03, 2015 at 01:25 AM
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
}
Sorry, what is "input.getmousebuttondown(0)" and "input.getmousebutton(0)"? where must I write it in my code?
can you tell me more detail the code? I cant understand where i must write variable longspeed and shortspeed and where I write the value longspeed = 12 and shortspeed = 3, what is "input.getmousebuttondown(0)" and "input.getmousebutton(0)"?
thats pseudocode
the actual ones are
if (input.Get$$anonymous$$ouseButtonUp(0))
which your already using in your code
and then just include between up and down the
if (input.Get$$anonymous$$ouseButton(0))
I assumed if you wrote all that top code you could fix the $$anonymous$$or issues of me not using the proper name with caps and such.
Your answer
Follow this Question
Related Questions
Imported skehup into unity not moving as I want!!! 2 Answers
Movement on the keyboard and mouse 0 Answers
How To Perform A Mouse Click On Game Object 5 Answers
Mouse controlled Laserpointer 1 Answer
OnMouseDown doesn't work 1 Answer