Can somebody help me fix this script?
Im trying to get the following script to work but it doesn't seem to work like it should. I want it to charge a shot and fire it with the charged force, in the moment it does not charge for some error in the script. Any help/tips is appreciated! code:
public Transform firepoint;
public GameObject pumpkin3;
public float accelaration;
public float multiplicator;
private float shotdelay;
private float Nextfire = 0.0f;
float speed;
public float chargetime = 0.0f;
bool charging;
void start()
{
charging = false;
multiplicator = 5f;
shotdelay = 0.5f;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
charging = true;
speed = 0f;
chargetime = 0f;
}
if (charging == true)
{
chargetime +=Time.deltaTime * multiplicator;
speed = chargetime;
}
if (Input.GetMouseButtonUp(0))
{
GameObject Go = Instantiate(pumpkin3, firepoint.position, firepoint.rotation) as GameObject;
Go.GetComponent<Rigidbody2D>().AddForce(new Vector2(accelaration * speed, accelaration));
Nextfire = Time.time + shotdelay;
charging = false;
}
if (Time.time > Nextfire)
{
chargetime = 0;
speed = 0;
}
}
}
Answer by _Game_Dev_Dude_ · Nov 19, 2015 at 02:23 PM
if (Time.time > Nextfire)
{
chargetime = 0;
speed = 0;
}
This right here is probably your problem. I don't know what you are trying to do with this block of code but what it is effectively doing is reseting your chargetime and speed every frame EVEN if you are currently charging.
I also don't see anywhere that you are changing your acceleration variable so unless you are editing it in the inspector, it will be 0 and will mess up your addforce code. Hope this helps.
Thank you for the answer! yes the accelaration variable is changed in the inspector.
Answer by Landern · Nov 19, 2015 at 02:25 PM
Well, your "start" method should have an upper case "S", that would stop your initialization of some of your variables.
You should probably use if else statements since your fall through if statements are getting hit probably when you don't want. An example is the first if that checks to see if the mouse button is down, if it is it sets charging to true, which is fine, this also makes the next if statement true and then in the next frame the same thing happens over and over resetting your variables like you just pressed the mouse button for the first time, particularly because your time and speed variables are reset to 0 every time a frame is rendered.
You may need to adjust your NextFire variable, but this should get you going:
void Update()
{
if (Input.GetMouseButtonDown(0) && charging == false)
{
if (Time.time > Nextfire)
{
charging = true;
speed = 0f;
chargetime = 0f;
}
}
else if (Input.GetMouseButtonDown(0) && charging == true)
{
chargetime +=Time.deltaTime * multiplicator;
speed = chargetime;
}
else if (Input.GetMouseButtonUp(0) && charging == true)
{
GameObject Go = Instantiate(pumpkin3, firepoint.position, firepoint.rotation) as GameObject;
Go.GetComponent<Rigidbody2D>().AddForce(new Vector2(accelaration * speed, accelaration));
Nextfire = Time.time + shotdelay;
charging = false;
}
}
Thank you! I changed my script to this and tried it in unity but it still doesn't seem to charge, but I noticed that the chargetime variable is not changing at all, like before, so maybe it has something to do with that? I also adjusted the Nextfire variable, which works now. Or do I need to charge the accelaration variable, like Game_Dev_Dude said? I'm sorry if these questions may seem like dumb questions, but I'm still very new to scripting (started 5 days ago).