• 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 Tim-A · Jan 03, 2013 at 11:27 PM · raycastwork

RayCast not working properly

I have a ray cast script. When I press control it makes a ray in the editor. I have the ray go through the monster and doesn't always lowers a monsters health. Only sometimes it does. Also if I get to close to the monster and touch him, his health goes down rapidly and I haven't even fired a ray. What did I do wrong? Here is my code.

     var distance : float = 30;
 
 function Update () 
 {
     
     if(Input.GetButton("Fire1")){
         
     var dir = transform.TransformDirection(Vector3.forward);
     var hit : RaycastHit;
     
     
     Debug.DrawRay(transform.position, dir * distance, Color.blue);
     
     print("You fired a ray");
     }
     
 
 if(Physics.Raycast(transform.position, dir, hit, distance))
     {
         if(hit.collider.gameObject.tag == "Monster")
         {
             health.Health -= 5;
         
         print("You Hit Him");
         }
      } 
 
 }
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 Lovrenc · Jan 03, 2013 at 11:32 PM

First of all. You only have DEBUG.DrawRay in your FIRE event. The way you have it set up now, you are fireing EVERY FRAME. So move your Physics.Raycast code (that is what really is doing the shooting) inside your "Fire event" brackets.

EDIT;

 var distance : float = 30;
 
 function Update () 
 {
 
     if(Input.GetButton("Fire1")){
 
       var dir = transform.TransformDirection(Vector3.forward);
       dir.Normalize();
       var hit : RaycastHit;
 
 
       Debug.DrawRay(transform.position, dir * distance, Color.blue);
 
       print("You fired a ray");
 
       if(Physics.Raycast(transform.position, dir, hit, distance))
       {
          if(hit.collider.gameObject.tag == "Monster")
          {
            health.Health -= 5;
  
            print("You Hit Him");
          }
        } 
     }
 }

EDIT2: Maybe your Debug ray was 2 long because you didnt normalize direction. EDIT3: Fixed code bug.

Comment
Add comment · Show 12 · 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 Tim-A · Jan 03, 2013 at 11:36 PM 0
Share

I edited my first question. Is that right?

avatar image Tim-A · Jan 03, 2013 at 11:54 PM 0
Share

It now doesn't rapidly go down when I'm close to him. Thanks for that. Also the ray still goes through the monster, but only lowers his health sometimes. Do you know what could cause that?

avatar image Tim-A · Jan 04, 2013 at 12:06 AM 0
Share

BCE0125: Invalid declaration type 'void' BCE0051: Operator '*' cannot be used with a left hand side of type 'void' and a right hand side of type 'float'. BCE0023: No appropriate version of 'UnityEngine.Physics.Raycast' for the argument list '(UnityEngine.Vector3, void, UnityEngine.RaycastHit, float)' was found.

Why?

avatar image Lovrenc · Jan 04, 2013 at 12:08 AM 0
Share

I had a bug in a code if you coppied it 2 fast. Hope it works now. I am really bad in unityscript.

avatar image Tim-A · Jan 04, 2013 at 12:14 AM 0
Share

I still has the same errors. :(

Show more comments

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Raycast doesn't work properly? 0 Answers

Why Raycast is not working properly? 0 Answers

Confused as to how my RaycastHit script works 1 Answer

Getting material index from raycast 2 Answers

Raycast and Layer Masks 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