I’m making a script that creates a empty game object in the center of a rigid body with a specific tag, then destroys it when the mouse is lifted.
Everything but the destruction part seems to work fine, so what’s wrong?
GameObject dragger;
private Camera fpsCam;
public float range;
// Use this for initialization
void Start () {
fpsCam = GetComponent<Camera>();
}
// Update is called once per frame
void Update () {
if(Input.GetButtonDown ("Fire1"))
{
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
if(Physics.Raycast (rayOrigin,fpsCam.transform.forward, out hit, range))
{
if (hit.transform.tag == "Grabbable")
{
Debug.Log("hit:" + hit.rigidbody);
dragger = new GameObject("dragger");
dragger.transform.position = hit.transform.position;
dragger.transform.parent = fpsCam.transform;
dragger.gameObject.tag = "dragger";
}
}
else
{
Debug.Log("not hit");
} if (GameObject.FindWithTag ("dragger")&& Input.GetButtonUp("Fire1"))
{
Destroy(GameObject.FindWithTag("dragger"));
}
}
}
}
I believe the GetButtonDown will trigger once per press, while GetButton will trigger while the mouse is held down. If you are trying to check while holding the mouse or fire down, try GetButton to have the ray check while held down. Let me know if that helps, thanks!