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 KathrynHazuka · Mar 11, 2015 at 09:20 AM · raycastsendmessage

Raycast check?

I am trying to create a lazer hit script that will tell the player that he is hit by the lazer. I am using the SendMessage called from the OutputLazer script when player intercepts the lazer. Before using this, i was using e boolean, that would set to true whe player intersect the lazer and flase when they do not. This worked untill you introduce 2 lazer outputs, becouse one of them will always say the player is not touching the lazer thus setting the boolean to false even if the other lazer is being intercepted.

I wish you could test if the player is being hit by a raycast from an object with a specific tag, that way all of this could be simplified to say if player is hit by raycast from this type of object, run this. But as far as i know thats not possible :(

I have been at it for 3 hours now and i just cant figure out a way to do it... i feel like im stupid... please help me.

here is the code for the OutputLazer script:

     // Update is called once per frame
     void Update () {
 
         //If lazer is on, test for any objects in the way
         if(lazerOn){
 
             if(Physics.Raycast(transform.position, -transform.forward, out hit)){
                 //Execute lazer collision when intersecting object has a collider
                 if(hit.collider){
                     LazerCollision();
                 }
             } else {
                 LazerNoCollision();
             }
         }
     }
 
     //Called when lazer hits a collider
     void LazerCollision (){
         lr.SetPosition(1, new Vector3(0,0,-hit.distance));
 
         //If lazer hits player, tell player they are in a lazer
         if(hit.collider.tag == "Player"){
             playerScript.SendMessage("InLazer");
         }
     }
 
     //Called when lazer hits nothing
     void LazerNoCollision(){
         lr.SetPosition(1, new Vector3(0,0,-50));
     }

Here are the snips of the code the Player script has containing the lazer portions:

     // Update is called once per frame
     void Update () {
 
         //Check if Player is falling off the map
         if (transform.position.y < 0.2){
             Die();
         }
 
         //Imitate player cooling down is player if hot
         if(rndr.material.color != playerColor){
             rndr.material.color = Color.Lerp(rndr.material.color, playerColor, Time.deltaTime * colorSmooth);
         }
 
         //If timer is above 0, decrease timer by seconds
         if(dieTimer > 0){
             dieTimer -= Time.deltaTime;
         }
     }
 
     //Runs when intersecting a lazer
     void InLazer(){
         //Imitate player heating up
         rndr.material.color = Color.Lerp(rndr.material.color, heatColor, Time.deltaTime * colorSmooth);
         //Increment death timer
         dieTimer += Time.deltaTime;
         LazerDeathCounter();
     }
 
     //Kills player and resets lazer values / player color
     void LazerDeathCounter(){
         if(dieTimer >= 0.5f){
             Die();
             rndr.material.color = playerColor;
             dieTimer = 0;
         }
     }

Comment
Add comment · Show 1
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 meat5000 ♦ · Mar 11, 2015 at 05:52 AM 0
Share

The raycasting object performs the check against what was hit by the raycast. It is easy enough to communicate with the hit object by interacting with the RaycastHHit info. There is much information on this.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Racast hit (send message with parameters) 0 Answers

Raycast message problem 1 Answer

Raycast SendMessage not working properly 1 Answer

how to make one script change a variable in another scipt 2 Answers

Raycast with SendMessage can send Parameter, but how to send a variable ? 2 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