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 leadpixel · Mar 23, 2011 at 05:05 PM · mousetimespeedclickcharge

Charging up the speed of an game object through a mouse click

Hi

i'm a bit of noob on unity and struggling a bit with my game at the moment due pretty much zero coding experience.

Basically what i want is to make a game object be able to increase its potential velocity (in my case its a sphere). I need it to charge up it's possible speed when i click down the left mouse button, and release its momentum when the button is released, gradually decreasing its speed as it moves forward. The release speed will increase depending on the length of time the button is depressed. Just so you know this is happening in a 3d environment and everything else is being controlled through the mouse i.e camera view, direction sphere is travelling ect.

As i said really haven't got much coding experience so if any could point me in the right direction with a bit of script it would be really appreciated.


Moved update progress to main question from an answer


hi,

Thanks for the link its been a big big help. i have how ever run in to abit of a road block again. It works perfectly on the first charge up but but won't reach its maximum charge on any atempts afterwards. If you could have a look at my script and give me some advice on how to fix it that would be great. Anyways here's what i managed to cobble together :

//create a private variable (not shown in inspector) to store the current set power private var thePower : float;

//create a boolean flag we can use to stop and start the addition to power private var increasing : boolean = false;

//create a boolean flag we can use to stop the player shooting during the shot private var shooting : boolean = false;

//Default, the charge will go up 1 per second var chargeSpeed : float = 1;

//set the maximum charge to be used for the maths function below var fullcharge : float = 10;

// create a number to multiply the force by as the value of up to 256 may not be enough to

// effectively shoot a ball forward var shotForce : float = 5;

var target: Transform;

function Update () {

     //if we are not currently shooting and Jump key is pressed down
     if(!shooting && Input.GetButtonDown("Jump")){
     increasing=true;

}

     // detect if Jump key is released and then call the Shoot function, passing current 
     // value of 'thePower' variable into its 'power' argument
     if(!shooting && Input.GetButtonUp("Jump")){
     //reset increasing to stop charge of the power bar
         increasing = false; 
         //call the custom function below with current value of thePower fed to its argument
         Shoot(thePower);    
     }
     if(increasing){
         //add to thePower variable using Time.deltaTime multiplied by barSpeed
         thePower += Time.deltaTime * chargeSpeed;

         //stop (or 'fight') thePower from exceeding fullWidth using Clamp
         thePower = Mathf.Clamp(thePower, 0, fullcharge);

         }

}

// start the 'Shoot' custom function, and establish a // float argument to recieve 'thePower' when function is called function Shoot(power : float){ //stop shooting occuring whilst currently shooting with this boolean flag shooting = true;

//find the forward direction of the object assigned to the spawnPos variable var fwd : Vector3 = transform.forward; target.rigidbody.AddForce(fwd power shotForce); yield;

 shooting = 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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Alexandre Zakime · Mar 23, 2011 at 05:09 PM

Hi!

you may want to look this: http://www.unity3dstudent.com/2010/10/football-kick-power-bar/#

Comment
Add comment · 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
1

Answer by Statement · Mar 27, 2011 at 06:42 PM

It works perfectly on the first charge up but but won't reach its maximum charge on any attempts afterward.

It seems this could would never reset the charge, but I can't see any problems with it getting maximum charge. I am not a JS guru but I think you should call Shoot with StartCoroutine. I'll edit the code below (I reformatted the indentation and removed the comments, sorry about that) and maybe you can have a quick test with it?

private var thePower : float; private var increasing : boolean = false; private var shooting : boolean = false; var chargeSpeed : float = 1; var fullcharge : float = 10; var shotForce : float = 5; var target : Transform;

function Update () { if(!shooting && Input.GetButtonDown("Jump")) { increasing = true; }

 if(!shooting && Input.GetButtonUp("Jump"))
 {
     increasing = false; 
     StartCoroutine(Shoot(thePower)); // Use StartCoroutine.
     thePower = 0;                    // Reset the power after a shot.
 }

 if(increasing)
 {
     thePower += Time.deltaTime * chargeSpeed;
     thePower = Mathf.Clamp(thePower, 0, fullcharge);
 }

}

function Shoot(power : float) { shooting = true; target.rigidbody.AddForce(transform.forward power shotForce); yield; shooting = false; }


However, if you're willing to go with another approach we might reduce some code.
Code is tested and works on my machine.

var target : Rigidbody; // Target to shoot away. var timer : float = 1; // 10 means it takes 10 seconds for full force. var minForce : float = 10; // Minimum force applied on shot. var maxForce : float = 50; // Maximum force applied on shot. private var power : float = 0; // Power charge.

for (;;) yield Play(); // Enter a Coroutine loop instead of Update.

function Play() {
power = 0; while (!Input.GetButton("Jump")) { yield; } // Wait until press. while ( Input.GetButton("Jump")) { Charge(); yield; } // Charge until release. Shoot(); }

function Charge() { power = Mathf.Clamp01(power + (1 / timer) * Time.deltaTime); }

function Shoot() { var impulse = Mathf.Lerp(minForce, maxForce, power); target.AddForce(transform.forward * impulse, ForceMode.Impulse); }

Comment
Add comment · Show 5 · 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 leadpixel · Mar 28, 2011 at 02:47 PM 0
Share

thats is amazing thank you so much, and no worries about the reformatting it needed it lol i'm just wondering if it would possible to attach a glow or light of somekind that would increase in intensity as the charge goes up though,as it seems like some visual indication of the charging process would be helpfull

avatar image leadpixel · Mar 28, 2011 at 02:48 PM 0
Share

btw i was willing to go with your approach lol

avatar image Statement · Mar 28, 2011 at 02:54 PM 0
Share

Sure, you could make an Update function that sets material color or something based on power. Power go from 0 to 1, which makes it ideal for interpolating values with Lerp ($$anonymous$$athf.Lerp, Vector3.Lerp, Color.Lerp etc)

avatar image Statement · Mar 28, 2011 at 02:55 PM 0
Share

function Update () { renderer.material.color = Color.Lerp(Color.white, Color.red, power); }

avatar image Statement · Mar 28, 2011 at 02:57 PM 0
Share

Or you could just set the color during Charge() so you only update it when you are charging. Also you'd want to reset it at the start of Play().

avatar image
0

Answer by leadpixel · Mar 27, 2011 at 06:25 PM

Moved reply to question. See above. /Statement

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

cannot change Cube Speed Over Time 1 Answer

OnMouseUp does not work 1 Answer

Move object towards mouse but if mouse moves object fallows it's original path 0 Answers

How to detect double and single clicks using javascript? 1 Answer

Player to look at 0 Answers


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