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 Borzi · Dec 01, 2013 at 03:46 PM · raycastcollidervector3hitlinecast

Problem with LineCast Bullet

Hey guys! I am trying to get this LineCast to detect a hit, but it just gets destroyed in instantiation, which is weird because no objects around the place where it is instantiated have colliders or are set to "ignore raycast". The bullet itself actually has no collider. Other than that the code works perfectly, which makes it furthermore frustrating...if someone could help me, it would be much appreciated! Thank you in advance.

 void FixedUpdate()
     {
     CurPos = myTransform.position;
     if(Physics.Linecast(PrePos, CurPos, out Hit) && expended == false)
     {
     if(Hit.collider.tag != "Bullet")
             {
             expended = true;
             myTransform.renderer.enabled = false;
             }
     }        
     PrePos = CurPos;            
     }
Comment
Add comment · Show 4
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 iwaldrop · Dec 01, 2013 at 07:34 PM 0
Share

Please post all relevant parts of the script. Nothing in this snippet calls destroy, so you must be calling it from somewhere else. What are the conditions that must be met to destroy the object?

avatar image Borzi · Dec 01, 2013 at 08:02 PM 0
Share

Well if it is "expended" and the mesh renderer is turned off, its basically destroyed.

avatar image iwaldrop · Dec 01, 2013 at 08:53 PM 0
Share

I don't know what expended means in your code, but just turning the renderer off doesn't equal destroyed. You say the code works perfectly, but it destroys itself on the first fixed update...that doesn't seem right.

I see that you check expended after the raycast. Either you should check it before the raycast or you should use the enabled flag so the updates stop running altogether. But aside from that tidbit, neither you nor your code provides enough information to solve the problem. Like the math questions in elementary school where x=1, x+y=z, what is z?

avatar image Borzi · Dec 02, 2013 at 11:47 AM 0
Share

Expended is quite simple actually, it basically checks if the Linecast has hit something and stops it from triggering on anything else. The problem I have is when I debug, I get a hit on an object which should not really be in the PrePos to CurPos range.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by keyp3r · Dec 01, 2013 at 11:38 PM

A possible issue could be that your bullet isn't tagged with "Bullet" That being said, you should probably be using layers and a layer mask. Something like this would be much better and probably fix your issue to boot:

 public LayerMask CastMask;
 
 Vector3 PrePos;
 bool expended = false;
 
 void Update(){
     //Move the object forward here...
     if(!exended){
         if(Physics.Linecast(PrePos, transform.position, out Hit, CastMask)){
             expended = true;
             myTransform.renderer.enabled = false;
         }
         PrePos = transform.position;
     }
 }

Then you can drive the whole thing using layers, much easier, cleaner and you have access to the physics settings matrix if you need. Just make sure you make a new layer for your bullet, assign it, and uncheck it in that LayerMask (as well as anything else you dont want your bullet checking against).

I would also suggest using Destroy(gameObject) in place of just turning off the renderer and checking a bool. The object is still there taking up memory, with it's colliders and what-not. But if you need it to be persistent post-expending then thats fine, just be sure to also do a collider.enabled = false; in there too.

Also, if none of that works, you may need to add a rigidbody to it. Just add one, un-check UseGravity and check all the constraint boxes.

Hope that helps! = )

Comment
Add comment · Show 1 · 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 iwaldrop · Dec 02, 2013 at 12:57 AM 1
Share
  • for layer and mask info.

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

18 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

Related Questions

Linecast from raycast hit point 1 Answer

Raycast, Linecast, sphere collider or capsule collider? 0 Answers

Raycast ignoring tags problem 1 Answer

Assigning a transform using the ray cast collider. 2 Answers

Hit distance Change light range 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