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 Steve.unity · Sep 21, 2014 at 01:40 AM · physics.overlapsphere

Help with raycasting code

This is my code for a raycast thats meant to display how far something is away from you when you click. but it doesnt change the "distance" vriable. can someone fix it for me? It also has another set of code but dont worry about it.

 #pragma strict
 
 var dmg : int = 50;
 var distance : float = 1.0;
 
 function update()
 {
     if (Input.GetButtonDown("Fire1"))
     {
         var hit : RaycastHit;
         if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit))
         {
             distance = hit.distance;
             hit.transform.SendMessage("Applydmg", dmg, SendMessageOptions.DontRequireReceiver);
         }
     }
  

Comment
Add comment · Show 5
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 robertbu · Sep 21, 2014 at 01:41 AM 0
Share

After pasting code, select the code and use the 101/010 button to format it (and the new lines will show up). I did it for you this time.

avatar image robertbu · Sep 21, 2014 at 01:43 AM 0
Share

Do you know if your Raycast() is hitting something?

avatar image Louis Watson · Sep 21, 2014 at 02:13 AM 0
Share

your RayCast is missing a fourth variable a float for the distance to send the ray.

ie:

 if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit, 100.0))
avatar image robertbu · Sep 21, 2014 at 02:20 AM 0
Share

@LouisWatson - a fourth parameter is not required. If not provided, it will default to $$anonymous$$athf.Infinity.

avatar image Lylek · Sep 21, 2014 at 02:41 AM 3
Share

function Update() not capitalized.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by philwinkel · Sep 21, 2014 at 05:02 AM

Raycast wont hit anything that does not have a collider. Make sure your geometry have some kind of colliders on them.

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 Steve.unity · Sep 21, 2014 at 11:56 PM 0
Share

So do i just have to turn $$anonymous$$esh collider on?

avatar image Erisat · Sep 22, 2014 at 12:01 AM 0
Share

It may be what Lylek23 said above, you did not capitalize Update() so it won't be called. change update() to Update(). And of course, you do need a collider, or the raycast won't hit anything, like philwinkel said.

avatar image Steve.unity · Sep 22, 2014 at 04:45 AM 0
Share

It was the capital :( almost 2 weeks of trying to figure it out and it was a capital u :(

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

physics.OverlapSphere and loops troubles 0 Answers

Mob AI not immediately finding object it is looking for, and crashing when it eats food? 1 Answer

Why is FindGameObjectsWithTag ignoring my Physics.OverlapSphere? 1 Answer

Prevent overlapping of spawned object over AI and Player 1 Answer

How to use Physics.OverlapSphere with layer mask? 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