Objects not responding correctly to Raycast

Hey guys.

I am currently working on a game where you are walking around in a sewer-labyrinth, and in order not to die from the poisonous environment, one must pick up anti toxins on the way. I have several of these potions in my scene, and they are tagged as “Health”. My Raycast script is using the “ScreenPointToRay” thingy, and the position is set as the middle point of the screen, where a small crosshair is placed. When this crosshair is hovering over such a potion, and a mouse button is clicked, the poison level of your body is decreased, and the potion os destroyed.

However, sometimes when I want to pick up a potion, I hover over it with my crosshair, but the potion os not responding to my mouse clicks. I have to sometimes click outside the actual object for it to work, and I was wondering why that is and how to fix it?

My code for the Raycast is this:

public var fence : GameObject;

public var fence2 : GameObject;

public var lever : GameObject;

public var wheel : GameObject;

private var opened : boolean = false;

private var opened2 : boolean = false;

private var openedError : boolean = false;

public var counter : float = 0;

public var distance : float = 5;

public var myStyle : GUIStyle;

public var player : GameObject;


function Update()

{


	var ray = Camera.main.ScreenPointToRay(Vector3(Screen.width/2, Screen.height/2, 0));
	

	var hit : RaycastHit;
	

	if( Physics.Raycast(ray, hit, distance) )

	{
	

		Debug.DrawLine(ray.origin, hit.point, Color.red);

		

		if(hit.collider.tag == "Lever")

		{
	

			Debug.Log("Hit");
			
			if(Input.GetMouseButtonDown(0)  opened == false)

			{
			

				opened = true;

				lever.animation.Play();

				fence.animation.Play();

				fence.audio.Play();

			
			}
		

		}

		else if( hit.collider.tag == "wheel" )

		{
		

			Debug.Log("Hit2");
			

			if( Input.GetMouseButtonDown(0)  opened2 == false )

			{
			

				opened2 = true;

				wheel.animation.Play(0);

				fence2.animation.Play();

				fence2.audio.Play();
			

			}

			else if( Input.GetMouseButtonDown(0)  opened2 == true )

			{
			

				openedError = true;
			

			}

		
		}

		else if( hit.collider.tag == "Health" )

		{
		

			Debug.Log("Health!");
			

			if( Input.GetMouseButtonDown(0) )

			{
			

				if(player.GetComponent(HealthScript).timer < 20)

				{

					player.GetComponent(HealthScript).timer = 0;

				}

				else{

				player.GetComponent(HealthScript).timer -= 20;

				}

				Destroy(hit.collider.gameObject);

			
			}
		

		}
	

	}


}


function OnGUI(){


	if( openedError == true  counter < 10 )

	{
	

		counter += Time.deltaTime;

		GUI.Label( Rect( Screen.width/2-250, Screen.height/2-75, 500, 200), "This wheel has already been turned!", myStyle);


	}
	

}

Of course I have colliders on the potions. I attached Sphere Colliders to them, and I have also tried to make these colliders a lot bigger than the object, but it seems like I still hav problems with this.

Any help is appreciated!

Thanks. :slight_smile:

So, it seems like even though I have the crosshair, and the ray is supposed to shoot at Screen.width/2 and Screen.height/2, the collision with the potions are calculated with the actual MOUSE position. At least that’s what it seems like.

If you guys wanna check it out for youself, to get a better idea of the problem, send me a message, and I can send you the game as it is at the moment. :slight_smile: