- Home /
Increased projectile velocity when holding button down 2D
I'm new to Unity and trying to make a Worms type game in 2 or 2.5d. So far, I have my script for shooting working, but it only shoots at the set velocity. I need to figure out how to increase the power based on how long the shoot button is held down.
Any help would be greatly appreciated. Thanks!
My current shooting code:
var Shell:Rigidbody;
var mag:float = 10;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
var newShell:Rigidbody;
newShell = Instantiate(Shell,transform.position,Quaternion.identity);
newShell.velocity = transform.TransformDirection(mag,0,0);
newShell.transform.Rotate(0,90,0);
}
}
Comment
Can you post your current shoot function. It will be easier to give a good answer if I can use your code in it.
Answer by Joshua · May 19, 2011 at 03:06 PM
Small example:
var force : float;
var increase : float = 1;
if (input.GetMouseButton (0)) {
force += increase*Time.deltaTime;
}
if (input.GetMouseButtonUp (0)) {
//your shoot function
force = 0; //reset the force
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612080830im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Shoot second cell in 2d 0 Answers
Physics in 2d arkanoid. Tangential and normal velocity 1 Answer
Objects moving faster after time 1 Answer