I am having this issue with raycasting where the 1,1,1 scale primitive object is being ignored …but when i size it up it works just fine.
i am trying to shoot an object with my gun , by detecting the hit via raycasting i want to reduce the health
i have two scripts “shoot” and “health” ,shoot is attached to my gun and health to the “dummy” object that is to be hit
so now when i use the standard size (1,1,1) object …debug prints hitting and hp is always 100
but on increasing the size …the hit is registered and hp is reduced
can anyone help me out ? is there a better more efficient way to do the same ?
public class health : MonoBehaviour {
public float hp=100f;
//public Material mat;
void Start () {
//mat = GetComponent<Material> ();
}
// Update is called once per frame
void Update () {
Debug.Log (hp.ToString ());
if (hp <= 0f)
{
Debug.Log ("dummy is dead");
//mat.color = Color.black;
}
}
}
public class shoot : MonoBehaviour {
public Vector3 point;
public float range; //set to 100
public float damage;//set to 10
void Start () {
point = transform.Find ("tip").gameObject.transform.position; // the tip of my gun
}
void Update () {
Ray ray = new Ray(point,Vector3.forward);
RaycastHit hit;
if (Input.GetMouseButton (0))
{
if(Physics.Raycast(ray,out hit,range))
{
Debug.Log ("hitting!!");
if(hit.collider.tag=="dummy")
{
Debug.Log("hitting dummy!!");
hit.collider.gameObject.GetComponent<health>().hp-=damage;
//hit.collider.gameObject.GetComponent<Renderer>().material.SetColor("colour",Color.red);
}
}
}
}
}