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 0l1v3r114 · Mar 19, 2014 at 04:22 AM · raycastdistancedamageattackmelee

Enemy will only take (melee) damage when the distance is below 1 (ie. A zero with decimals after it)

Well I said it all in the title. I'm an absolute beginner to Unity and I'm following the Brackeys tutorial on youtube and no matter how high I set the max distance in the inspector tab or in the actual code it only takes damage when its a zero with decimals as I said. Any help would be much, much appreciated. Thanks

Here's the Code for the player:

 #pragma strict
 
 var TheDammage : int = 50;
 var Distance : float;
 var MaxDistance : float = 20.0;
 
 function Update ()
 {
     if (Input.GetButtonDown("Fire1"))
     {
         var hit : RaycastHit;
         if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
         {
             Distance = hit.distance;
             if (Distance < MaxDistance)
             {
                 hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
             }
         }
     }
 }
 

And for the Enemy:

 #pragma strict
 
 var Health = 100;
 
 function ApplyDammage (TheDammage : int)
 {
     Health -= TheDammage;
 }
Comment
Add comment · Show 3
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 getyour411 · Mar 19, 2014 at 05:03 AM 0
Share

I haven't used hit.distance (wasn't even aware of it to be honest) so there might be something off about the way you are using it. For a test, try the more generic

 Distance = Vector3.Distance(hit.position,transform.position);
 if(Distance < ...
avatar image robertbu · Mar 19, 2014 at 05:28 AM 0
Share

@getyour411, @supernat - If you don't specify a distance in the Raycast(), it uses $$anonymous$$athf.Infinity for the distance.

avatar image supernat · Mar 19, 2014 at 06:17 PM 0
Share

@robertbu - good call, I didn't think about the fact it uses infinity, but the only example in unity script passes a value in for depth and then uses the returned depth, so I would try it just to rule out any Unity oddity.

I thought you might be getting the $$anonymous$$axDistance from the editor ins$$anonymous$$d of the one in code too, actually that was my first thought, but you said you were changing it in the inspector. I would just make it private like robertbu said to rule it out. If you're using a prefab and you didn't update the value on the prefab, that could explain it as well.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by supernat · Mar 19, 2014 at 05:20 AM

You need to add a distance to the RayCast call (probably MaxDistance plus a little), after passing the parameter hit. Otherwise, it is likely limiting the returned distance to 1.0 since that is a unit vector magnitude.

Comment
Add comment · 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
0

Answer by robertbu · Mar 19, 2014 at 05:26 AM

I have an educated guess here. I'll bet you are trying to change the distance by changing this line:

  var MaxDistance : float = 20.0;

Instance variables like this one are public by default. They appear in the Inspector. The initialized value (20 in this case) is only used at the time the script is attached to the game object. After that, only the value in the Inspector matters. So you can either change the value in the Inspector, or you can make this variable private:

 private var MaxDistance : float = 20.0;

Private variables will not appear in the Inspector and will be initialized by the code in the script.

Comment
Add comment · 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

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

Melee - Raycasting Distance, Sending Message 1 Answer

Player Attack Script. I Need Help! 0 Answers

melee attack with raycast 0 Answers

Melee Damage script by collision 2 Answers

NullReferenceException: Object reference not set to an instance of an object Raycast...? 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