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 iamthecoolguy11 · Mar 09, 2014 at 06:48 AM · raycastsendmessagec# tutorial

simple way to send a message threw a raycast

So what im trying to do is send a message to a other script and have it send how much damage to cause it and to call a function.

heres the sending script

 public int speed = 5;
         public Transform hold;
         public Transform aim;
         public bool aiming = false;
         public float FireRate;
         public RaycastHit hit;
         public GameObject Barrel;
         public int dammage; 
 
     void Start() {
         InvokeRepeating("fire", 2, FireRate);
     }
     // Update is called once per frame
     void FixedUpdate () 
     {
         
         if(Input.GetKey(KeyCode.Mouse1))
         {
             float step = speed * Time.deltaTime;
             transform.position = Vector3.MoveTowards (transform.position, aim.position, step);
         }
         else
         {
             float step = speed * Time.deltaTime;
             transform.position = Vector3.MoveTowards (transform.position, hold.position, step);
         }
 
         if(Input.GetKey(KeyCode.Mouse0))
         {
             Vector3 fwd = transform.TransformDirection(Vector3.forward);
             
             print("MouseLook button down");
             if (Physics.Raycast(Barrel.transform.position, fwd, 1000))
             {
                 hit.collider.SendMessage("hit", SendMessageOptions.DontRequireReceiver);
                 print("fired");
             }
         }
     }
     }

here is the receiving script

 public float hp = 2.0f;
 
 public void hit()
 {
     hp-=1;
     if(hp<=0)
     {
         Destroy(gameObject);
     }
 }
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 mattyman174 · Mar 09, 2014 at 07:25 AM 0
Share

Are you having any specific problem or do you have a question?

1 Reply

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

Answer by JNetRocks1316 · Mar 09, 2014 at 07:42 AM

I believe what you're looking for is RaycastHit information.

Can't really go into specifics without knowing what you're trying to hit/scripts you're trying to look for. Like if you're looking for an enemy, you need to determine which enemy you hit, then access the script component that holds the health information for that enemy.

This should be a good starting point though.

 using UnityEngine;
 using System.Collections;
 
 public class RayCastDamage : MonoBehaviour {
 
     void FixedUpdate()
     {
         //Create a RaycastHit so we can store the information from our Raycasts
         RaycastHit hitInfo; 
 
         if(Input.GetKey (KeyCode.Mouse0))
         {
             Vector3 fwd = transform.TransformDirection (Vector3.forward);
 
             if(Physics.Raycast (Barrel.transform.position, fwd, 1000, out hitInfo))
             {
                 //Save the raycast information into hitInfo
                 if(hitInfo.collider.tag == "Player"){
                     //Check if the ray hit the player
                     //You can then get the script attached the player (or whatever object)
                     //And call the funcation to do damage.
                 }
             }
         }
     }
 }
Comment
Add comment · Show 4 · 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 iamthecoolguy11 · Mar 09, 2014 at 07:59 AM 0
Share
 , out hitInfo

that being on the end of the thing makes the script get a error

avatar image JNetRocks1316 · Mar 09, 2014 at 08:05 AM 0
Share

Get rid of the word out and try it with just hitinfo. You're basically just telling it where to save that hitInfo (which is a type RaycastHit into the RaycastHit we created earlier, called hitInfo). Should work!

avatar image iamthecoolguy11 · Mar 09, 2014 at 05:44 PM 0
Share

that did not work

avatar image iamthecoolguy11 · Mar 09, 2014 at 07:59 PM 0
Share

i found what you are trying to do this is what it is it is the same thing so i well give you it

 RaycastHit hit;
         if (Physics.Raycast(transform.position, -Vector3.up, out hit))
             float distanceToGround = hit.distance;
         

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

Want Object A to send a message to Object B when it is directly above it 3 Answers

'SendMessage' is not a member of 'UnityEngine.RaycastHit'. 1 Answer

Findout if an object that was hit by raycast is still being hit (30 object that are instantiated) 0 Answers

Raycast message problem 1 Answer

Racast hit (send message with parameters) 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