- Home /
Trouble with raycast hitting a tagged object
I am trying to apply damage to an enemy(zombie) when it enters the attack range of a tower. Firstly I have set it up so the raycast works and only goes a certain amount of range, when the zombie enters the range, the tower looks at it. But now I am trying to kill the zombie.
Sorry if this code is sloppy, I haven't been doing this for long. As you can see in the function attack(), I am trying to make the ray detect what it is shooting with a tag, but the Debug.log is not showing up when it is in range.
var distance;
var targetTransform : Transform;
var attackRange = 15.0;
var moveSpeed = 5.0;
var Damping = 6.0;
var fireRate = 0.4;
var allowedShot : boolean = true;
var damage = 100;
var hit : RaycastHit;
function Update ()
{
targetTransform = GameObject.FindWithTag("Zombie").transform;
distance = Vector3.Distance(targetTransform.position, transform.position);
if (distance < attackRange)
{
attack();
}
if (distance > attackRange)
{
}
Debug.DrawRay(transform.position, transform.forward * attackRange, Color.red);
}
function attack()
{
var rotation = Quaternion.LookRotation(targetTransform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
forward = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, forward, hit, attackRange) && allowedShot)
{
if (hit.collider.gameObject.tag == ("Zombie"))
{
Debug.Log("zombie hit");
//hit.transform.gameObject.SendMessage("ApplyDamage", 100, SendMessageOptions.DontRequireReceiver);
allowedShot = false;
reload();
}
}
}
function reload()
{
yield WaitForSeconds(fireRate);
allowedShot = true;
}
I have got it to work by removing the "if (hit.collider.gameObject.tag == ("Zombie"))" statement, but that is not what I want as the tower then begins to destroy anything in its path.
I have just copied/pasted your code and it worked fine for me. I can only imagine you have not set the tag correctly on the Zombie GameObject, or you have multiple objects tagged with "Zombie" and that's caused an issue.
@hamstar This could be the problem, The zombie I have set up is a prefab I made. I was unaware that you couldn't have multiple of the same tag. I will look into this now.
It's not that you can't have multiple objects with same tag, it's just you need to change your logic a bit. An easier way might be to have an invisible sphere collider in a child object of the tower to detect enemies. Set the collider size to the attack range you want, then attach a script that uses OnTriggerEnter or OnTriggerStay to attack a zombie. $$anonymous$$eep transforms of zombies that come in range in a list and add/remove them when they enter/leave the collider.
@hamstar Thank you so much, I didn't even think of that method and I have used it before.
Answer by meat5000 · Sep 28, 2013 at 11:18 AM
function Update ()
{
targetTransform = GameObject.FindWithTag("Zombie").transform;
This could potentially find a new target each frame.
Also, in your Raycast
Physics.Raycast(transform.position, forward, hit, attackRange)
try a more complete expression of 'forward', like Vector3.forward.
Thanks for the reply, I have put the "targetTransform = GameObject.FindWithTag("Zombie").transform;" into a Start funtion, It seems to be working the same as before.
I also changed the forward expression. but it is still not working.
Answer by hamstar · Sep 28, 2013 at 01:04 PM
I think there is a mistake when you assign the value to "forward". Vector3.forward is the world forward Vector, not the object's local forward.
Try this:
var forward : Vector3 = transform.TransformDirection(transform.forward);
I just used updated that part, no errors. But that is not the problem i am having, I have debugged the raycast line so it shows in the scene view. It is facing the right way and going the right distance away. but it is not registering when the zombie actually gets touched by the ray.
O$$anonymous$$, I'm not sure that it makes a difference, but you don't need the brackets around zombie: .tag == "Zombie"
.
It didn't make a difference but thanks for the suggestion
Your answer
Follow this Question
Related Questions
Disabling a lineRender or Ray when there is no target 1 Answer
Controlled Automatic Fire Rate (RayCasting) 2 Answers
How to make 3D texts show ammo? 4 Answers
GetComponent() problems 2 Answers
Raycasting fail 1 Answer