- Home /
Raycasting ignoring small sized object
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);
}
}
}
}
}
Answer by Legend_Bacon · Aug 13, 2018 at 03:45 PM
Hello there,
In your Ray, you have Vector3.Forward, which is the same as "Vector3(0, 0, 1)".
This means that it doesn't take your gun orientation into account.
Instead, you should probably have point.transform.forward. Since "point" is the tip of your gun, this will make the ray go forward from the tip.
Note: This is assuming your "point"'s forward is actually pointing forward. To make sure, select the tip during gameplay, and in local mode make sure the blue arrow point in the correct direction (in front of it).
Also, to debug the ray you should probably check out DrawRay().
I hope that helps!
Cheers,
~LegendBacon
Hey thanks for the answer,i haven't tried it out yet ..but wouldn't point.transform.forward cause an error since point has already been declared as a vector3 ?
Ah, my mistake, I thought it was a gameobject...
So if I understand it correctly, you define that "point" vector on start, but then never assign it again? That means that that Vector is always going to have the same value, regardless of player movement or rotation.
i would declare a gunTip gameObject, assign it and use that with my answer above.
Also, make sure you put your Ray declaration inside the Input IF statement, so you don't declare a ray you're not going to use on every frame!
I hope that helps!
Cheers,
~LegendBacon
Your answer
Follow this Question
Related Questions
(C#) Collider to collider2d 0 Answers
What's wrong with my RaycastHit2D? 1 Answer
How to check if a raycast is not hitting any tagged colliders? 0 Answers
Problem with raycast distance 1 Answer