Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by valdiazdanovic · Apr 03, 2015 at 01:13 AM · mouseclickclicktomove

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;
     //=======================================================
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
 }


Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image valdiazdanovic · Apr 03, 2015 at 01:38 AM 0
Share

Sorry, what is "input.getmousebuttondown(0)" and "input.getmousebutton(0)"? where must I write it in my code?

avatar image valdiazdanovic · Apr 03, 2015 at 02:58 AM 0
Share

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)"?

avatar image sparkzbarca · Apr 03, 2015 at 03:15 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges