• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How can I determine how many colliders exist along a line? 1 Answer

making ray ignor certain collider 1 Answer

How can I apply damage based on a grenade explosion, but test whether the objects are behind cover so they are not damaged even within the explosion radius? 2 Answers

Raycast for 2d with touch. 0 Answers

Casting a Ray in the direction of the movement 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges