- Home /
"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);
}
}
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
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 :)
This is not going to do it or only if you know that the item is moving less than 0.1 unit per frame.
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!
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.
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.