I’m having a strange problem. I want to make it so if the space bar is pressed, the player shoots a projectile every few seconds. I’ve tried 2 techniques so far, and the common theme seams to be that if the projectile is set to instantiate every 0 seconds, it explodes like crazy; anything greater, it makes a solid line with some glitchy movements.
Here are some screenshots: Imgur: The magic of the Internet
If you can think of any way I could space out the projectiles more, or why this is happening it would be very much appreciated.
using System.Collections;
public class player : MonoBehaviour {
private Transform ship;
public int shipspeed = 5;
public Transform shotPos;
public float shotForce = 1000f;
public Rigidbody projectile;
bool con = true;
void Start () {
//spawn position
ship = transform;
ship.position = new Vector3(0, -7, 2);
}
void Update () {
//Horizontal movement with arrow keys
ship.Translate(Vector3.right * shipspeed * Input.GetAxis("Horizontal") * Time.deltaTime);
//ship wrapping
if(ship.position.x > 6){
ship.position = new Vector3(-6, -7, 2);
}
else if(ship.position.x < -6){
ship.position = new Vector3(6, -7, 2);
}
//gun
if(Input.GetButton("Jump")){
StartCoroutine("Fire");
}
if(Input.GetButtonUp("Jump")){
StopCoroutine("Jump");
}
}
//luanch projectile
void Shoot (){
Rigidbody shot = Instantiate(projectile, shotPos.position, shotPos.rotation) as Rigidbody;
shot.AddForce(shotPos.up * shotForce);
}
//gun delay loop
IEnumerator Fire(){
while(con == true){
Shoot ();
yield return new WaitForSeconds(1f);
}
}
}