Quantcast
Channel: Questions in topic: "hold"
Viewing all articles
Browse latest Browse all 154

Shoot laser while holding down key

$
0
0
Hey, in my game I want to shoot a laser when the player is holding down a key (this is for Android, so when the player is holding down a GUITexture), I'm using raycastAll for the effect since I want it to penetrate throught everything, but once it hits another player, it stops the raycast. This is my script: using System.Collections; public class NeonShoot : TouchLogic { public Player playerScript; public float direction = 1f; public GameObject graphics; void Update () { if (playerScript.neonStrike == true) { this.guiTexture.enabled = true; //target.guiTexture.enabled = true; } else { this.guiTexture.enabled = false; //target.guiTexture.enabled = false; graphics.GetComponent().enabled = false; } } void OnTouchBegan () { touchToWatch = TouchLogic.currTouch; } void OnTouchStayed () { if(TouchLogic.currTouch == touchToWatch) { if (playerScript.neonStrike == true) { graphics.GetComponent().enabled = true; RaycastHit2D[] hits; hits = Physics2D.RaycastAll(transform.position, Vector2.right * direction , 100.0F); for (int i = 0; i < hits.Length; i++) { hits[i].transform.SendMessage("Hit", SendMessageOptions.DontRequireReceiver); if (hits[i].transform.tag == "Player") { playerScript.neonStrike = false; } } } } } void OnTouchEnded () { touchToWatch = 64; playerScript.neonStrike = false; } } It's inheriting from TouchLogic, all that there is in touchLogic are send Messages basically, so I can call functions inside a script using it. Anyway this script isn't working, when the neonStrike bool is true, the GUI textures appear, but when I click it, nothing happens, maybe the problem actually is on the TouchLogic script, here it is btw: using UnityEngine; using System.Collections; public class TouchLogic : MonoBehaviour { [HideInInspector] public int touchToWatch = 64; public static int currTouch = 0; public GameObject target; void Update () { if (Input.touches.Length <= 0) { //no touches } else { //if there are touches, oop through all the touches //loop through all the touches for (int i = 0; i < Input.touchCount; i++) { currTouch = i; //executes this code for current touches(i) if (this.guiTexture != null && this.guiTexture.HitTest(Input.GetTouch(i).position)) { //if current touch hits our GUI Texture if (Input.GetTouch(i).phase == TouchPhase.Began) { this.SendMessageUpwards("OnTouchBegan", SendMessageOptions.DontRequireReceiver); } if (Input.GetTouch(i).phase == TouchPhase.Ended) { this.SendMessageUpwards("OnTouchEnded", SendMessageOptions.DontRequireReceiver); } if (Input.GetTouch(i).phase == TouchPhase.Moved) { this.SendMessageUpwards("OnTouchMoved", SendMessageOptions.DontRequireReceiver); } if (Input.GetTouch(i).phase == TouchPhase.Stationary) { this.SendMessageUpwards("OnTouchStayed", SendMessageOptions.DontRequireReceiver); } } if (Input.GetTouch(i).phase == TouchPhase.Began) { this.SendMessageUpwards("OnTouchBeganAnywhere", SendMessageOptions.DontRequireReceiver); } if (Input.GetTouch(i).phase == TouchPhase.Ended) { this.SendMessageUpwards("OnTouchEndedAnywhere", SendMessageOptions.DontRequireReceiver); } if (Input.GetTouch(i).phase == TouchPhase.Moved) { this.SendMessageUpwards("OnTouchMovedAnywhere", SendMessageOptions.DontRequireReceiver); } if (Input.GetTouch(i).phase == TouchPhase.Stationary) { this.SendMessageUpwards("OnTouchStayedAnywhere", SendMessageOptions.DontRequireReceiver); } if (target != null && target.guiTexture != null && target.guiTexture.HitTest(Input.GetTouch(i).position)) { //if current touch hits our target GUI if (Input.GetTouch(i).phase == TouchPhase.Began) { this.SendMessageUpwards("OnTouchBeganOnTarget", SendMessageOptions.DontRequireReceiver); } if (Input.GetTouch(i).phase == TouchPhase.Ended) { this.SendMessageUpwards("OnTouchEndedOnTarget", SendMessageOptions.DontRequireReceiver); } if (Input.GetTouch(i).phase == TouchPhase.Moved) { this.SendMessageUpwards("OnTouchMovedOnTarget", SendMessageOptions.DontRequireReceiver); } if (Input.GetTouch(i).phase == TouchPhase.Stationary) { this.SendMessageUpwards("OnTouchStayedOnTarget", SendMessageOptions.DontRequireReceiver); } } } } } } So I hope you know what's wrong with it, Thanks in advance. edit: I think I'm getting closer to the wished result :) I just started a NeonShoot script from scratch debugging basically everything to see what could cause the problem, I'll continue tomorrow though, need to sleep :) edit2: Okay, today seems that it's not working... the code is the same as yesterday, wth is happenning?!?! edit3: I deleted the GUITexture gameObject I was using yesterday and created a new one, assigned the script to it and each of the variables, working correctly again...

Viewing all articles
Browse latest Browse all 154

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>