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 Dogg · Jun 06, 2014 at 05:55 AM · raycast

How To Invoke RayCastAll?

Let me explain. I want to know how to invoke a RayCastAll script of mine to activate after a specific amount of time after I collect a certain powerup. Then deactivate after 2-3 seconds or so, and then repeat the process. The RayCastAll script of mine destroys objects in front of me when I attach it to the Main Camera, so I'm trying to get it to activate only after a certain amount of time after I collect a powerup of mine(I just restated what I just type earlier, sorry). So how can I achieve this? No invoke tutorials that I found are really that helpful, and there are very few of them on the internet and here; so I hope someone can help me. I've already tried all the scripts and references that I could find with the help of my own knowledge, but I haven't been able to come up with much.

If you don't fully understand what I want/need to happen, then let me put it in steps. Step 1. Player grabs powerup, timer starts. Step 2. Timer ends, causing my RayCastAll script to activate for 2-3 seconds. Step 3. 2-3 seconds are over, RayCastAll script deactivates, and that's it. Then you just repeat the steps. Here's my scripts to help you guys see how I set things up:

 RayCastAll script:

 public class DestroyObstacle : MonoBehaviour {
 
 
     void Update() {
         RaycastHit2D[] hits;
         hits = Physics2D.RaycastAll(transform.position, transform.forward, 100.0F);
 
         Debug.DrawRay(transform.position, transform.forward * 100, Color.red);
 
         int i = 0;
         while (i < hits.Length) {
             RaycastHit2D hit = hits[i];
             Renderer renderer = hit.collider.renderer;
             if (renderer) {
                 renderer.material.shader = Shader.Find("Transparent/Diffuse");
                 Color color = renderer.material.color;
                 color.a -= 0.3F;
                 renderer.material.color = color;
                 Invoke("LaunchProjectile", 2);
             }
             i++;
             Destroy(hit.collider.gameObject);
         }
     }
 }

 public class PowerupScript : MonoBehaviour {
     
 
     void OnTriggerEnter2D(Collider2D other)
     {
                 if (other.tag == "Player") {
                         ControllerScript playerScript = other.gameObject.GetComponent<ControllerScript> (); // not sure about the syntax here...
                         if (playerScript) {
 
                                 // call function on playerScript to set player to invincible
                                 playerScript.SetInvincible ();
                                 
                                 // We speed up the player and then tell to stop after a few seconds
                                 playerScript.SpeedUp();
 
                                 // We speed up the player and then tell to stop after a few seconds
                                 //playerScript.coeffSpeedUp = 5.5f;
                                 //StartCoroutine (playerScript.StopSpeedUp ());
 
 
                         }
                         Destroy (gameObject);
                 }
         }
     
     }

I also have this new invoke script that I just made(it's incomplete):

 ublic class InvokeScript : MonoBehaviour {
 
     public int secs2Wait = 3;
     public int[] totalSec = new int[3];
     
     void Start()
     {
         InvokeRepeating ("TimerInvoke", 1, 1);
     }
     
     void TimerInvoke()
     {
         if (totalSec [0] < secs2Wait)
                         totalSec [0]++;
         else
             CancelInvoke("TimerInvoke");
     }
 }

If you want my controller script, please feel free to ask. :D

EDIT: Wow this question is old, and still nobody knows a solution to this? This script and what I wanted to do can still be very useful to me. So if somebody knows, please feel free to answer.

Comment
Add comment
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

1 Reply

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

Answer by robertbu · Jul 21, 2014 at 03:38 AM

I takes some time to puzzle through what you are asking. Rather than InvokeRepeating, structure things this way. At the top of the file put:

  private bool canRaycastAll = false;

Put something like this at the top of Update():

  if (!canRaycastAll)
      return;

Then when you get your power up, you would do:

  canRaycastAll = true;
  Invoke("CancleRaycastAll", 2.5f) 

An CancleRaycastAll would be:

 void CancleRaycastAll() {
     canRaycastAll = false;
 }

There are other ways of doing what you want. This is only one of them, but probably the easiest to implement in your code above. The one sticking point may be where you detect the collision. If it is is another script on another game object, there is more to do to get this working.

Comment
Add comment · Show 1 · 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
avatar image Dogg · Jul 22, 2014 at 02:57 AM 0
Share

Sorry I didn't notice but the first huge code is actually two separate scripts. So there's the DestroyObstacle script and the PowerupScript. Calling canRaycastAll from the PowerUp script won't work like this. I just get The name canRaycastAll' does not exist in the current context`. Again it's my fault sorry.

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

21 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 avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Raycast Destroys player. 1 Answer

Why this script is not working? 1 Answer

hide child object script - help 1 Answer

button question 1 Answer


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