In the mobile 2D game that I’m developing the user touches the screen to shoot. So my code for that is basically:
function Update(){
for (var touch : Touch in Input.touches) {
if (Input.touchCount > 0 && touch.phase == TouchPhase.Began && Time.time >nextFireTime){
//Raycast Bullet, Fire Sound, Whatever
}
}
Now when I want to do something like open a door I’d touch that and it would open. But the problem is that I also have my function to fire every time the user touches the screen. So whenever the user touches to open a door, touches a gun icon to reload, or anything else in that manner -(I’ll call them “None firey stuff” for the sake of this question)- a bullet is fired when It really isn’t appropriate to.
So want to have the script not fire when “None firey stuff” is touched but I can’t figure it out.
I’ve tried boolean’s that sets to true when the “None firey stuff” is touched but by the time the firebullet script notices it it’s to late and the bullet has already been fired.
Then I’ve tried, and currently working on, using raycasts to ignore layers and only fire when it has hit a layer that is not ignored. Here’s what my script basically looks like for that:
for (var touch : Touch in Input.touches) {
if (Input.touchCount > 0 && touch.phase == TouchPhase.Began && Time.time > nextFireTime) {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (touch.position);
if (Physics.Raycast(ray,Mathf.Infinity,layerMask2)) { //This part makes sure that the user is touching a "Fire Friendly" area
//Play Sound
nextFireTime = Time.time + shotInterval; //nextFireTime and shotInterval is a var
if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, 1000, layerMask)) { //Shoots ray for Raycast Bullet
// If hit Gives damage, instantiates blood, whatever
}
}
}
}
But this doesn’t work either because if the player touches my little gun reload icon, which is a gui element, the raycast doesn’t even see it so I get firing while reloading. To try to fix this I tried putting a collider in the position of the reload icon on the screen and put the layer to that of “None firey stuff” but that doesn’t work at all.
So, how would you make it so it doesn’t shoot when a “None firey” object is touched.
Thank You for the help, you have no idea how much i appreciate it.
…
Edit:
Don’t have to look at it, just posting it if it helps
I might get asked this later so here’s what a fraction of my actual script I’m using for Firing pretty much looks like. :
function FixedUpdate(){
if (alive){
for (var touch : Touch in Input.touches){
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (touch.position);
var hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist))
{
var targetPoint = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
transform.Translate((Vector3.forward * moveSpeed) * Time.deltaTime);
}
}
}
}
function OnGUI(){
if (GunSlot == 1){
if (Weapon1 == 1){
AmmoText = CurrentBullets + "/" + TotalBullets;
if (GUI.Button (Rect (Screen.width/IconHorizontalPosition, Screen.height/IconverticalPosition, Screen.width/IconSize, Screen.height/IconSize), GunIcon, custom)){
if (GunTotalBullets >= 17){
if (GunCurrentBullets < 17){
if (!GunReloadBoolean){
StartCoroutine("ReloadGun");
}
}
}
else if (GunTotalBullets > 0){
if (GunTotalBullets < 17){
if (!GunReloadBoolean){
StartCoroutine("ReloadGunLessMag");
}
}
}
TempMag = 0;
GunMagazine = 17;
}
GUI.Label (Rect (Screen.width/HorizontalPositionAmmo, Screen.height/verticalPositionAmmo, Screen.width/SizeAmmo, Screen.height/SizeAmmo), AmmoText, custom);
for (var touch : Touch in Input.touches) {
if (Input.touchCount > 0 && touch.phase == TouchPhase.Began && Time.time > nextFireTime) {
if (GunCurrentBullets > 0){
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (touch.position);
if (Physics.Raycast(ray,Mathf.Infinity,layerMask2)) {
audio.PlayOneShot(Glock17ShotSound);
nextFireTime = Time.time + ShotInterval1;
if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, 1000, layerMask)) {
var gunProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation);
gunProjectile.velocity = (hit.point - transform.position).normalized * 200;
gunProjectile.transform.rotation = Quaternion.LookRotation(glock17Projectile.velocity);
var otherObj : GameObject = hit.collider.gameObject;
if ((otherObj.tag == "Zombie") || (otherObj.tag == "MediumZombie") || (otherObj.tag == "FastZombie")) {
var healthScript : ZombieHealth = otherObj.GetComponent("ZombieHealth");
if (healthScript.angle < 8.0){
var head = otherObj.transform.Find("Head");
Instantiate(bloodSplatter, head.position, Quaternion.LookRotation(hit.normal));
}
healthScript.HealthDamage(Damage);
}else{
Instantiate(gunShotParticle, hit.point, Quaternion.LookRotation(hit.normal));
}
}
GunCurrentBullets--;
}
}
}
if (GunCurrentBullets < 1){
if (GunTotalBullets >= 17){
if (!Glock17ReloadBoolean){
StartCoroutine("ReloadGlock17");
TempMag = 0;
GunMagazine = 17;
}
}
else if (GunTotalBullets > 0){
if (GunTotalBullets < 17){
if (!GunReloadBoolean){
StartCoroutine("ReloadGunLessMag");
TempMag = 0;
GunMagazine = 17;
}
}
}
}
}
}
}
}