- Home /
Shoot laser while holding down key
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<SpriteRenderer>().enabled = false;
}
}
void OnTouchBegan () {
touchToWatch = TouchLogic.currTouch;
}
void OnTouchStayed () {
if(TouchLogic.currTouch == touchToWatch) {
if (playerScript.neonStrike == true) {
graphics.GetComponent<SpriteRenderer>().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...
I tried debugging some stuff, seems that OnTouchBegan and OnTouchStay aren't being called correctly/when they should
i think im a bit confused but ill try to help
touchToWatch = TouchLogic.currTouch;
this seems like a case of trying to use a class like an instance.
I see the class TouchLogic I dont see the instance of the class your modifying.
TBH I don't understand how any of those TouchLogic calls are working.
NeonShoot inherits from touchlogic so i could see a case of
touchToWatch = currTouch
Because currtouch is an inherited variable.
unless your trying to get the actual touch logic instances curr touch which is differnet in which case its
touchToWatch = getcomponent<TouchLogic>().currTouch;
could you explain?
That's exactly what I want, but TouchLogic is already in NeonShoot since I'm inherting from TouchLogic, and the TouchLogic script is inherting from $$anonymous$$onoBehaviour, so by simply using TouchLogic.currTouch since it's actually static it should work without using getComponent I suppose :p
Answer by MadJohny · Dec 22, 2013 at 12:25 PM
Okay the problem is fixed, I forgot that I couldn't call update inside teh neonShoot script because it's already called in the TouchLogic script (since neonShoot is inherting from TouchLogic)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Joystick movement Android 2D 0 Answers
[I REALLY NEED HELP FAST]Help with enemy Shooting 1 Answer
Programmatically changing brightness setting on Android using c# 0 Answers