- Home /
How to limit bullets in screen like old NES game
Hello am trying to limit bullet in screen like Journey to Silius (NES game)
http://www.youtube.com/watch?v=9GG7fcVD18I
this game can shot only 3 bullet but after bullet off the screen or collide with something
will able to shoot 3 shot again
this is my code:
////////////////
Control.cs
`using UnityEngine; using System.Collections;
public class Control : MonoBehaviour {
public GameObject BulletPrefab;
public int weapon = 2;
public static int bulletCount =0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
print(bulletCount);
if(Input.GetKeyDown("w"))
{
if(bulletCount <3)
{
bulletCount++;
pistol();
}
}
}
void pistol(){
Instantiate(BulletPrefab, transform.position, transform.rotation);
}
}
/////////////////////////////////////// bullet.cs
using UnityEngine; using System.Collections;
public class Bullet : MonoBehaviour {
public float BulletSpeed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float toMove = BulletSpeed * Time.deltaTime;
transform.Translate(Vector3.forward * toMove);
Destroy(gameObject,2);
Control.bulletCount--;
}
void OnCollisionEnter(Collision collision) {
Destroy(gameObject);
Control.bulletCount--;
}
}
Answer by Berenger · Jan 30, 2012 at 05:32 PM
Well, you're almost there. Add a box Collider around the camera (orthographic right ?), make sure the sides of the box are on the side of the frustrum, tag it and enable isTrigger. In your bullet script, in the function OnTriggerExit, check the tag and destroy.
bulletCount was behaving properly so far ?
Answer by dowsodsai · Jan 30, 2012 at 10:01 PM
bulletCount not really properly at update function it keep minus bulletCount maybe because
of call once per frame, so do you have any idea where to put bulletCount when the bullet
didnt hit anything and destroy by timer ( onCollisionEnter bulletCount work pretty decent )
bulletCount at function update seem to be the only problem can we fix this ?
thank for your help.
Answer by dowsodsai · Jan 30, 2012 at 10:01 PM
bulletCount not really properly at update function it keep minus bulletCount maybe because
of call once per frame, so do you have any idea where to put bulletCount when the bullet
didnt hit anything and destroy by timer ( onCollisionEnter bulletCount work pretty decent )
bulletCount at function update seem to be the only problem can we fix this ?
thank for your help.
Answer by dowsodsai · Jan 30, 2012 at 10:00 PM
thank for your help
bulletCount at update function is the only problem because it call every frame so
bulletCount keep counting minus I don't sure how to fix this