- Home /
Need help fixing an error. Using raycast to mimic laster beam but no collider, Line Renderer, Raycast Collider.
I want to build a laser beam tower to attack the villains. If there was only one laser tower, it was straightforward. However, I can't hurt my enemy, I had Line Renderer in my object, do i need to add a raycast collider?
I wrote code like this "if(hit.collider.gameObject.tag == "LaserProjectile")" in my enemy's code.
Laser Code :
var hit : RaycastHit;
Physics.Raycast(transform.position, transform.forward, hit , attackingRange,Space.World);
if (hit.collider){
lineRenderer.SetPosition(1,Vector3(0,0,hit.distance));
}else{
lineRenderer.SetPosition(1, Vector3(0,0, attackingRange));
}
even though I can add "Destroy(hit.gameObject);" in the Laser code, nothing happen....
I really appreciate your help~
Gizmos are just for editing/debugging; they're not intended for in-game use. You'll probably want to use something like LineRenderer ins$$anonymous$$d. As for the exception you mention, please indicate which line of code generates the error; that'll make it easier for us to help. (I see some potential logic errors in your code, but I'm not sure if they're directly related to the problem.)
Thanks for your comment, the problem is "if(hit != null && hit.collider.gameObject.tag == "Enemy") Destroy(hit.collider.gameObject);" , if I delete this two sentenses, no error occur, but no enemy was attacked. BTW, thanks for your recommend, I will try LineRenderer, Thank you $$anonymous$$r. Jesse.
$$anonymous$$aybe you've destroyed the enemy's collider if it's a child of the root game object ins$$anonymous$$d of the whole thing. Generally with tower defence games you'd want to use gameObject.Send$$anonymous$$essage("ApplyDamage",10); ins$$anonymous$$d to deal damage to the enemy ins$$anonymous$$d of removing it. (you need a function called ApplyDamage() on the enemy for this to work)
Thanks spinaljack. But the problem is I can get (hit.collider) in Laser.js only, how to Send$$anonymous$$essage to Enemy.js? how to make sure which enemy was hurted? Thanks for your help..
I read your question, and I tried to use Getcomponent to control enemies' HP, "if(hit.collider.gameObject.tag == "Enemy") hit.collider.gameObject.GetComponent("$$anonymous$$oving.js").HP -= 10;" However, the error is "HP is not a member of UnityEngine.Component".. Please help me $$anonymous$$OTORO..
Answer by denewbie · Nov 06, 2010 at 02:54 PM
if(hit != null && hit.collider.gameObject.tag == "Enemy") <--- if the hit was null the second part will have a null error so you have to split the 2 to either
if (hit != null) if (hit.collider.gameObject.tag == "Enemy") Destroy(hit.collider.gameObject);
This way you check if the hit was null first before you check for the collier.gameObject so you wont get the error.
No, if statements leave early when something fails. It needs to be written as
if ( hit && hit.gameObject && hit.gameObject.tag == "Enemy" )
though I'm pretty sure hit can't be non-null while hit.gameObject is null.
Thank you guys. Then I use GetComponent to decrease HP of villans. var target : $$anonymous$$oving = hit.collider.gameObject.GetComponent("$$anonymous$$oving"); target_BTS.ApplyDamage (); this seems to work fine.