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
-1
Question by 3CardMonte · May 11, 2012 at 01:06 AM · collisionraycastdamagerayshooter

Detecting Raycast collision on an enemy to deal damage

Background:

Creating a 3rd person shooter and I have the hero shooting his pistol which creates a ray since the bullets move too fast for projectiles, ect.

I want the robot to be able to take damage when hit. This is what I have but it doesnt seem to be working. Im REALLY new to scripting so I'm kind of lost but I need to do this for a project. The main problem I'm having is that the robot is written in javascript but the player is in c# since they were assets from the asset store..

as for the code:

For the gun shooting the ray (c#):

 void OnFire () {
     Vector3 dir = aimTarget.position-aimPivot.position;
     dir.Normalize();
     Ray ray = new Ray(aimPivot.position, dir);
     RaycastHit hit;
     if (Physics.Raycast(ray, out hit, 1000, mask)) {
         hit.transform.root.SendMessage("OnHit", new RayAndHit(ray, hit), SendMessageOptions.DontRequireReceiver);
     }
 }



As for the robot..

 function OnRayCollision(rayCollision : RayCollision)

{ if (rayCollision.ray.CompareTag("robot")) hitpoints=0; }

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

Answer by Berenger · May 11, 2012 at 03:34 AM

I don't know what OnRayCollision is, but it's not from Unity, unlike OnTrigger... or OnCollision...

A message is send to the target. If there is a script implementing the function OnHit taking a RayAndHit in parameters attached to the same GameObject as the collider being hit, it will be called. Make sure sure there is such a function, that the spelling is right and that the parameter is correct.

Comment
Add comment · Show 3 · 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 3CardMonte · May 11, 2012 at 01:56 PM 0
Share

I read up more on this subject since it seems like it's been asked many times, however I still cannot get it to work. I tried your suggestions and the gun shoots the ray, but I'm still having trouble with the robot detecting being hit by it. (I set the damage to instant kill the robot for testing purposes)

 function Update () {
     var hit : RaycastHit;
     if (Physics.Raycast (hit)) {
         hitPoints = 0;
     }
 }

In short, nothing happens and I actually get the following error:

Assets/WeaponScripts/CharacterDamage.js(18,25): BCE0023: No appropriate version of 'UnityEngine.Physics.Raycast' for the argument list '(UnityEngine.RaycastHit)' was found.

avatar image 3CardMonte · May 14, 2012 at 01:13 AM 0
Share

I really cant figure this out,it's not like I havent tried, because I've spent a few days trying to fix it.. can ANYONE help?

avatar image syclamoth · May 14, 2012 at 01:16 AM 2
Share

You need to provide more information about the ray to be cast! Simply saying 'raycast' isn't enough. It's like saying 'go', but not telling it what direction to go in!

 if(Physics.Raycast(transform.position, transform.forward, hit))
 {
     // shot a ray out the front!
 }

And no, it's not because you haven't tried- it's because you haven't read the scripting reference! It's very clearly laid out there how all this works. You should really have read it before even posting here in the first place!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unreliability of Physics.Raycast.Distance for player movement? 0 Answers

detect Ray Crossing? Intersecting? 1 Answer

How would I find the closest Vector3 point to a given Ray? 2 Answers

Raycast goes through 1 Answer

Player takes damage on collision with an enemy 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