- Home /
if mouse button is pressed continual add force from min. to max.and then shoot a ball
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); }
}
Answer by Statement · Dec 15, 2010 at 03:20 AM
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); }
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.
@madmike6537 It should fire when you release the button. Just make sure that autoThrow is set to false. (Press, hold & release)
Thanks! I ended up getting this to work perfectly, forgot to update my comment! Thanks again :)
Answer by Justin Warner · Dec 15, 2010 at 12:30 AM
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...
Thanks for your help but Im not able to fix the problem....could someone write this code clean
Edited answer to be more neat and in the Update function...
$$anonymous$$any thanks for your fast answer.Is there a fast way to make it reverse if i leafe the mouse button??
That doesn't make any sense Dave. You wanted to have continual force between $$anonymous$$ and max. How are you ever going to shoot it "half way through"? Sit down and think through what it is you really want.
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....
Your answer
Follow this Question
Related Questions
[Solved] How to shoot an object in front of the player? 1 Answer
Applying force to an object without rotating it. 2 Answers
Shoot at Mouse Cursor 1 Answer
Rigidbody force problem 1 Answer
Add force on camera using AddForce 2 Answers