- Home /
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;
}
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?
Well if it is "expended" and the mesh renderer is turned off, its basically destroyed.
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?
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.
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! = )
Your answer
Follow this Question
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