• 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
1
Question by Crackpotweevil · Feb 24, 2015 at 06:19 AM · collisioninstantiatedestroycontact.point

"Destroy" bullet without turning off trail renderer

SO I've got a game where you shoot bullets at other players in slow motion. You can deflect bullets with other bullets much like in the movie "Wanted". To help communicate to the player where the bullets are I put some trails on them. However, if I destroy the game object the trails disappear.

I created this script that checks the collision with the wall, instantiates a bullet hole on the wall, and then turns off the mesh renderer & mesh collider. However when I shoot a wall the collider does not shut off until after it has collided so I end up with invisible trails bouncing to the ground.

Any Suggestions?

Here's the code

 void OnCollisionEnter(Collision collision)
     {
         if(collision.gameObject.name == "Wall")
         {
             // Make sure the shot has made contact
             Debug.LogWarning("Collided with wall");
 
             // Find the contact point so we can instantiate the Bullet hole facing the player
             ContactPoint contact = collision.contacts[0];
             Quaternion rot = Quaternion.FromToRotation(Vector3.down, contact.normal);
             Vector3 pos = contact.point;
 
             // Create the Bullet hole facing the player
             Instantiate(bulletHole, pos, rot);
 
             // Destroy the bullet WITHOUT turning off the trail renderer
             renderer.enabled = false;
             collider.enabled = false;
             Destroy(gameObject, 5);
         }
     }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Pixzle · Feb 24, 2015 at 07:48 AM

What you wanna do is to predict if the bullet will hit the wall. You can do that by raycasting forward from the bullet, and if the ray hits a wall say, 0.1 units away, the collider is turned off.

I can give you an example later when I get home.

That can find the docs for raycasting here: http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

Comment
Add comment · Show 5 · 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 Crackpotweevil · Feb 25, 2015 at 05:31 PM 0
Share

Thanks buddy! I'm working on it right now but I think I can make this work. I've been working so much latley I barely have time to build my own game so I've been doing 30 $$anonymous$$ute sprints with my breakfast haha

I'll let you know when (if) I get it working :)

avatar image fafase · Feb 25, 2015 at 05:36 PM 0
Share

This is not going to do it or only if you know that the item is moving less than 0.1 unit per frame.

avatar image Crackpotweevil · Feb 25, 2015 at 06:09 PM 0
Share

I have to go to work now, however I did get it working... sort of.

The ray casting worked, but because I was turning off the collider before the bullet actually collided, I could no longer use my contact points to instantiate the bullet holes.

So I changed it around to make the raycast instantiate the bullet hole, but it looks like my bullets aren't co$$anonymous$$g out of my gun straight so now the bullet holes are a couple units off of the true contact point.

OH I also had to put a limit to how many bullet holes could be created because it was making like 9 of them every time.

I'll work on it some more tomorrow but as far as the question goes, it's answered :) Thanks!

avatar image Pixzle · Mar 07, 2015 at 08:47 PM 0
Share

no problems! can you mark my answer as answer?

avatar image fafase · Mar 07, 2015 at 10:24 PM 0
Share

Your answer is not right so why would he? As I said, if you raycast at a certain distance from a point, it is just a matter of time before you run out of luck.

  "You can do that by raycasting forward from the bullet, and if the ray hits a wall say, 0.1 units away, the collider is turned off."

What if the bullet moves 0.2 unit per frame and a wall is 0.05 unit large? One frame, the bullet is on one side, next frame, it is on the other side and no collision returned.

avatar image
0

Answer by fafase · Feb 25, 2015 at 06:03 PM

 private Vector3 prevPos = Vector3.zero;
 private RaycastHit hit;
 
 private bool CheckCollision(){
    if(prevPos == Vector3.zero){
       prevPos = transform.position;
       return false;
    }
    return Physics.Linecast(prevPos, transform.position, out hit);
 }

Then you can do:

  if(CheckCollision()){Debug.Log(hit.point);}

This is keeping track of previous position and creating a line between the previous and the current position. If something in between you get it.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Instantiate 1 object after 2 objects collide. ( C# ) 1 Answer

Handling two objects collisions in the right order 1 Answer

Instantiate and Destroy an object while crossing a line with its clone also 1 Answer

Creating a Switch that Controls a Gate 1 Answer

Destroy() not working on collision 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