# if mouse button is pressed continual add force from min. to max.and then shoot a ball

 0 Hallo is there anybody who will help me with this code??? ``````var cannonball : Transform; var power = 0; var speed = 5; var powermax = 5000; var powermin = 100; var timeToMove : float = 0.5; // time to move (in seconds) private var lerpAmt : float = 0.0; // current lerp 't' amount (private, 0-1) if ( Input.GetButtonUp("Fire1") // if pin is pressed && lerpAmt < 1 ) // and lerpAmt is not already at max { lerpAmt += Time.deltaTime / timeToMove; power = Mathf.Lerp(powermin, powermax, lerpAmt); } function Update() { if(Input.GetButton("Fire1")) { var projectile = Instantiate(cannonball, transform.position, transform.rotation); projectile.rigidbody.AddForce(transform.right * power);//cannon's x axis Physics.IgnoreCollision(projectile.collider, collider); } } `````` more ▼ asked Dec 14 '10 at 11:52 PM Dave M 20 ● 3 ● 3 ● 7 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 ``````if ( Input.GetButtonUp("Fire1") // if pin is pressed && lerpAmt < 1 ) // and lerpAmt is not already at max { lerpAmt += Time.deltaTime / timeToMove; power = Mathf.Lerp(powermin, powermax, lerpAmt); } `````` That isn't in a function, it needs to be so. ------------EDIT------------------ ``````var cannonball : Transform; var power = 0; var speed = 5; var powermax = 5000; var powermin = 100; var timeToMove : float = 0.5; // time to move (in seconds) private var lerpAmt : float = 0.0; // current lerp 't' amount (private, 0-1) function Update() { if ( Input.GetButtonUp ( "Fire1" ) && lerpAmt < 1 ) // and lerpAmt is not already at max { lerpAmt += Time.deltaTime / timeToMove; power = Mathf.Lerp ( powermin, powermax, lerpAmt ); } if ( Input.GetButton ( "Fire1" ) ) { var projectile = Instantiate ( cannonball, transform.position, transform.rotation ); projectile.rigidbody.AddForce ( transform.right * power );//cannon's x axis Physics.IgnoreCollision ( projectile.collider, collider ); } } `````` That's it cleaned up and in the Update function... more ▼ answered Dec 15 '10 at 12:30 AM Justin Warner 6.3k ● 19 ● 27 ● 65 Thanks for your help but Im not able to fix the problem....could someone write this code clean Dec 15 '10 at 01:39 AM Dave M Edited answer to be more neat and in the Update function... Dec 15 '10 at 02:14 AM Justin Warner Many thanks for your fast http://answer.Is there a fast way to make it reverse if i leafe the mouse button?? Dec 15 '10 at 03:19 AM Dave M That doesn't make any sense Dave. You wanted to have continual force between min and max. How are you ever going to shoot it "half way through"? Sit down and think through what it is you really want. Dec 15 '10 at 03:22 AM Statement ♦♦ Ok sorry, I mean If I hold the mouse button continual and the force goes to max. in 5 sec.( and I still hold the button but the force doesent rise) and if I leaf the mouse button the force goes down to 0. And the ball will be pushed. Hope you understad that.... Dec 15 '10 at 03:46 AM Dave M add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 Try this script. I tested it and it seems to work just fine. If you don't want to automatically shoot when powermax is reached, uncheck Auto Throw in inspector. Easy. ``````var cannonball : Transform; var speed : float = 500; // speed is power per second var powermax : float = 5000; var powermin : float = 100; var autoThrow : boolean = true; function Update() { if (Input.GetButtonDown("Fire1")) StartCoroutine("Cook"); } // This is a co-routine. // I used the term "to cook a grenade" as popular in many action games. function Cook() { var power : float = powermin; while (Input.GetButton("Fire1")) { var move = speed * Time.deltaTime; power = Mathf.MoveTowards(power, powermax, move); if (autoThrow && power == powermax) break; else yield; } Shoot(power); } function Shoot(power : float) { var projectile = Instantiate(cannonball, transform.position, transform.rotation); projectile.rigidbody.AddForce(transform.right * power); Physics.IgnoreCollision(projectile.collider, collider); } `````` more ▼ answered Dec 15 '10 at 03:20 AM Statement ♦♦ 20.2k ● 35 ● 71 ● 176 It works great!!!Many thanks Dec 15 '10 at 03:54 AM Dave M Darn,I am trying to use this but I need it to fire when you release the button, I dont believe this functions that way. Nov 16 '12 at 11:31 PM madmike6537 @madmike6537 It should fire when you release the button. Just make sure that autoThrow is set to false. (Press, hold & release) Dec 03 '12 at 11:17 PM Statement ♦♦ Thanks! I ended up getting this to work perfectly, forgot to update my comment! Thanks again :) Dec 03 '12 at 11:58 PM madmike6537 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x1002
x330
x302
x255

asked: Dec 14 '10 at 11:52 PM

Seen: 1718 times

Last Updated: Dec 03 '12 at 11:58 PM