Hi, I’ve made a raycast script with a layer called collisionMask as hit detection on the target:
Ray ray = new Ray (spawn.position, -spawn.right);
RaycastHit hit;
float shotDistance = 20;
if (Physics.Raycast (ray, out hit, shotDistance, collisionMask)) {
shotDistance = hit.distance;
healthBar.BarScale();
I have a different script on a health bar game object that i want to scale every time the collisonMask is hit:
float myScale = -5f;
public void BarScale()
{
transform.localScale = new Vector3(myScale, 1, 1);
}
}
The issues are that right now the health bar only scales once on the first hit. I want it to scale every time it hits. And lastly it scales the wrong way, it becomes bigger instead of smaller even though my float is minus. Does anyone see the problem(s)? I’d really appreciate the help
Scaling doesn’t work like that, your scale needs to be smaller than the original, i think it was 1 at the beginning so you could use 0.8f as your “myScale”. If you scale an object to a negative size, that means that it flips over on that axis and scales like it would normally, so a -5 value flipped your object and scaled it to 5 times the original.
Ahaa wonderfull! that makes sense. Thank you that clears up half the issue now I just need to get it to repeat the scaling every time the ray cast hits collisionMask
If you want to do that, use this instead:
transform.localScale=newVector3(transform.localScale.x - myScale, 1, 1);
and set the scale to the amount with you want to scale down. if you set it to 0.2, the scale will change with 0.2 every time (it will scale with the 1/5 of the original object)