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 CHHOST · Jan 05, 2013 at 07:15 PM · raycastbulletcube

raycast and detecting if it did hi

Hello im curently making an FPS shooter. And my bullets are done by using raycast.

Now i want to make computer controlled bots, and i need them to be able to take damage from the user who shoots the raycast. Therefore i would like to know if it is possible to check if a give gameobject (bots, but lets call it a cube for testing purposes) is hit by a raycast.

have been searching google and the docs but cant seem to find anything, so would be nice if someone would help me :)

thanks in advance.

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
0

Answer by MaGuSware™ · Jan 05, 2013 at 07:24 PM

I don't think it's possible to check if something is hit by a raycast but you can check to see if the raycast has hit something.

When you are doing your raycast, include a RaycastHit variable which will contain the hit information of the raycast. Then you act upon that accordingly.

Here is an extract from my gun script:

In my "shoot" function

 RaycastHit hit;
 if(Physics.Raycast(rayOrigin, rayDirection, out hit, m_fMaxRange))
 {
     ProcessRayHit(hit);
 }

And my ProcessRayHit function (stripped down)...

 void ProcessRayHit( RaycastHit hit )
 {
     hit.collider.gameObject.SendMessage("DoDamage", m_iDamage, SendMessageOptions.DontRequireReceiver);
 }

To send damage to my targets I use send message with "DoDamage" as the function name.

Some helpful links:
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html

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 CHHOST · Jan 05, 2013 at 07:33 PM 0
Share

i have the following code:

 if (distance > 0) {
         var hit : RaycastHit;
 
         if (Physics.Raycast(oldPos, direction, hit, distance)) {
             // adjust new position
             newPos = hit.point;
             // notify hit
             hasHit = true;
             var rotation : Quaternion = Quaternion.FromToRotation(Vector3.up, hit.normal);
             
             //Apply force if we hit rigidbody
             if (hit.rigidbody){
                 hit.rigidbody.AddForce( transform.forward * impactForce, Force$$anonymous$$ode.Impulse );
             }
         
 //HIT SURFACES
             if(impactHoles){
                 //var impact : GameObject;
                 for(var i : int = 0; i<impactObjects.Count; i++){
                     if(hit.transform.tag == impactObjects[i].name){
                         Instantiate(impactObjects[i], hit.point, rotation);
                     }
                 }
             }
             //We cant hit ourselfs
             if(hit.transform.tag != "Player" && doDamage){
                 hit.transform.Send$$anonymous$$essageUpwards("ApplyDamage", damage, Send$$anonymous$$essageOptions.DontRequireReceiver);
             }
         
             Destroy (gameObject, 1);
         }
     }

and my dmg is handled by var damage : int = 20;

so how would i implement it?

avatar image MaGuSware™ · Jan 05, 2013 at 07:41 PM 0
Share

well, you need a receiver script on what it is that you want to damage which will have a function like so

 function ApplyDamage( damage : int )
 {
     //do the damage stuff here.
 }

$$anonymous$$ine is like this

 public class Health : $$anonymous$$onoBehaviour 
 {
     public int m_i$$anonymous$$axHealth = 100;
 
     int m_iCurHealth;
     bool m_bAlive = true;
 
     void Start() 
     {
         m_iCurHealth = m_i$$anonymous$$axHealth;
     }
 
     public void DoDamage( int damage )
     {
         if (m_bAlive)
         
             m_iCurHealth -= damage;
         
             if (m_iCurHealth <= 0)
             {
                 //Do $$anonymous$$ill
             }
         }
     }
 }

avatar image CHHOST · Jan 05, 2013 at 08:03 PM 0
Share

ok i now have the following: in my bullets script:

if(hit.collider){ hit.collider.gameObject.Send$$anonymous$$essage("DoDamage", damage, Send$$anonymous$$essageOptions.DontRequireReceiver); }

and my hp script: public int hp = 100;

 public int currentHp;
 bool alive = true;
 
 void Start(){
     currentHp = hp;    
 }
 
 public void DoDamage(int damage){
     if(alive){
         // then we are alive
         currentHp -= damage;
         if(currentHp <= 0){
             // then we are dead
             alive = false;
         }
     }
 }

still does not seem to work?

avatar image MaGuSware™ · Jan 05, 2013 at 09:41 PM 0
Share

did you keep it as a C# class, or make it into a JS? Because if you made it into a JS that syntax is wrong.

JS should be

 var currentHp : int = 100;
 private var alive : bool = true;

 function DoDamage(damage : int)
 {
     if (alive)
     {
         currentHp -= damage;
         if (currentHp <= 0)
         {
             alive = false;
         }
      }
 }

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

9 People are following this question.

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

Related Questions

Bullet collision for space shooter - colliders, rays, etc? 2 Answers

Physics.Raycast Interval problem in the FixedUpdate(). 0 Answers

What is this variable type? 2 Answers

Use transform.LookAt with Raycast with a delayed bullet? 1 Answer

Ray curve for bullet gravity effect, or any way to make it 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