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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by oldsouldier · May 13, 2012 at 05:07 PM · raycasttagdetectionname

Raycast detection / lag issue

Hey everyone, I have a raycast issue, and believe me I have trolled these forums looking for an answer to my specific problem, hitherto I haven't found someone with the same problem I am having.

Here is my simple code which casts a ray forward of the "gun" and is attempting to detect what object is in front of it (by name, but I've tried by tag / Comparetag):

pragma strict

function Update () {

var length = 10;

 var fwd = transform.TransformDirection (Vector3.forward);
 
 var hit : RaycastHit;
 
 if (Physics.Raycast (transform.position, fwd, hit)) {
   
               var nameof = hit.collider.name;
 
                 print (nameof);
  
 }
   Debug.DrawRay(transform.position,fwd * length, Color.red); 
 
 
 }


There are two problems I am having with this code: 1) It will detect the name of the object, but only the first time it strikes the object and will stay "detected" until I strike a new object. So if I hit terrain, it will stay terrain until I hit cube, or some other object. 2) It is causing an unhealthy tick in the movement of the main character. When I rapidly turn or something to that effect, it will cause a bad "tick" to form. (I tried using pragma stict, but it didn't help).

I have been looking through the forums for a while and didn't find any solutions that helped, if you have an idea please let me know and thanks for your time in advance.

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 whydoidoit · May 13, 2012 at 05:16 PM 0
Share

Just let me check - where does the value stay the same? You are not calling print unless there is a collision, so it definitely won't get updated - you'd need to do something in the "else" of the if(Physics.Raycast.

Or am I missing the point?

A glitch could be caused by what's in print, especially if it takes a long time.

avatar image oldsouldier · May 13, 2012 at 05:24 PM 0
Share

Well, I shuffled the code around to this:

pragma strict

function Update () { var nameof; var length = 10;

 var fwd = transform.TransformDirection (Vector3.forward);
 var hit : RaycastHit;
 if (Physics.Raycast (transform.position, fwd, hit)) {
   
   nameof = hit.collider.name;
                
 }
 else{
   nameof = null;
 }
   
   
   Debug.DrawRay(transform.position,fwd * length, Color.red); 
   print(nameof);
  
 }


And it is causing the same issues (I switch to null on the else because it was crashing with it being set to hit.collider.name).

avatar image whydoidoit · May 13, 2012 at 07:58 PM 0
Share

Yeah hit will be null if it didn't hit something. I'm very surprised that it isn't printing null a lot though when it doesn't hit.

BTW you can just use transform.forward rather than creating that vector fwd.

0 Replies

· Add your reply
  • Sort: 

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

Why isn't hit.transform.name with ray working? 1 Answer

How to assign a Variable with a Raycast 1 Answer

Name and tag 1 Answer

Instantiating gameObejcts that can't be referred to after creation 1 Answer

Trigger and raycast error? 2 Answers


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