Else not working in a raycast. need help

First time asking question

In this script I have a tower that when clicked a ring shows up and shows the Diameter that it can shoot in. I want the script to be able to tell when I click the tower, and have the ring appear but not make another ring if clicked again. I also want the script to be able to tell if I am clicking on the object or not, and if not destroy the ring (by destroying all rings) and making it possible for that tower to create another ring

this is my script, any help would be greatly appreciated.

private var closed = true;
var Ring:Rigidbody;
var offset:Vector3;
var target1: Transform;

function Update () 
{

}

function OnMouseDown ()
{
	if (closed)
	{
		closed = false;
		var newRing:Rigidbody;
		newRing =
Instantiate(Ring,transform.position+offset,Quaternion.identity);	
	}
	
	var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	var hit: RaycastHit;

	if (Physics.Raycast(ray, hit)) 
	{
		if (hit.transform == target1)
		{
			print("Hit target 1");
		}	
	
		else
		{	
			print("Hit nothing");
			var Foo : GameObject[];
			Foo = GameObject.FindGameObjectsWithTag("Radius");
			for (var i in Foo )
			Destroy(i);
			closed = true;	
		}
	}
}

Could be to do with floating point imprecision... Maybe check against object rather than position of object...

Is there any collider you click on if it's not on the tower? Your else statement only calls when the raycast hits something. So if you click on literally nothing, not even a floor collider it won't work. If you don't want such a collider, try to move the else after the if statement of the raycast, because the raycast will return false.

I'm not completely sure of what you want, but the way you have it structured, your 'else' clause only fires when 1) you hit something and 2) that something is not 'target1'. You may need to move your 'else' out one level so that it is the 'else' of the Raycast().

still working on it, changed it up a bit. I figured out why the else isnt working. There is no ray being cast if not clicking on the tower. Added this so I could see where I was clicking Debug.DrawLine (ray.origin, hit.point); noticed there were no lines being drawn as I clicked anywhere but the towers

1 Answer

1

I think you have the wrong understanding how Physics.Raycast works. The method returns true when it hit something. If it doesn’t hit anything, it will return false.

In your code you check inside the “i hit something” block of the raycast if the thing you’ve hit is your target. Your else will only be executed when you hit something but your target. If you did not hit anything you do nothing at the moment.

You might want to combine the two conditions into one like this:

if (Physics.Raycast(ray, hit) && hit.transform == target1)
{
    print("Hit target 1");
}
else
{
    print("Hit nothing or something else");
    // [...]
}

If you want to keep it like you had it, you have to consider the 3rd case as well:

if (Physics.Raycast(ray, hit))
{
    if (hit.transform == target1)
    {
        print("Case 1: Hit target 1");
    }
    else
    {
        print("Case 2: Hit something else");
    }
}
else
{
    print("Case 3: Hit nothing at all");
}

If you still have trouble understanding what Raycast returns, you might want to read the documentation (once more) carefully.