Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MadJohny · Dec 21, 2013 at 09:04 PM · c#androidshootingholdraycastall

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...

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MadJohny · Dec 21, 2013 at 10:10 PM 0
Share

I tried debugging some stuff, seems that OnTouchBegan and OnTouchStay aren't being called correctly/when they should

avatar image sparkzbarca · Dec 22, 2013 at 02:09 AM 0
Share

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?

avatar image MadJohny · Dec 22, 2013 at 10:51 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges