Efficiency with raycasting from mouse position

Hey all.

Im working on a little hobby project. Heres the overall idea.

The scene contains an array of tiles. Everytime I mouse over a tile, it should move slightly downwards and maybe play a sound or something like that. Its not totally fleshed out yet. So I started building it, and I do raycasting from the mouse position to see if im mousing over a tile. The problem is that with they way im doing it, Im raycasting all the damn time, and as soon as I start doing alot of tiles, the framerate drops horribly. I know im doing it wrong, im just not sure what the right way would be. Any help is appreciated. Code is seen below

using UnityEngine;
using System.Collections;

public class MouseRayCast : MonoBehaviour {
public Hashtable ht = new Hashtable();
public Hashtable sht = new Hashtable();
	// Use this for initialization
	void Awake () {
	
	ht.Add("y",-1.0f);
	ht.Add("time",0.5f);
	sht.Add("y", 0.0f);
	sht.Add("time", 0.5f);
	//All this hashtable stuff is because I use iTween to move my tiles upon mousing over
	}
	
	// Update is called once per frame
	void Update () {
		

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

RaycastHit hitInfo; 

 if (Physics.Raycast(ray, out hitInfo,50)){
	GameObject myBox;
	myBox = hitInfo.transform.gameObject;
	iTween.MoveTo(myBox,ht);
	}else{
	iTween.MoveTo(gameObject,sht);
			
		}
		
	}
}

Another problem is the logic I use to get the tiles to snap back into position once the mouse leaves that tile. The movement isnt very smooth, and tends to get jumpy because I basically have two conflicting iTween calls, causing hysteresis. Any tips on that would be awesome aswell.

In the link I’m about to provide you is a grid system built in c# for unity3d. It works as is in the current version of unity and does exactly what you’re looking for (and more). The reason I’m presenting you with the project as opposed to just answering the question is because it demonstrates more than one of the features you mentioned above in a very efficient manner. This tool kit is also released under the WTFPL license, which makes it very useable for your own purposes.

http://forum.unity3d.com/threads/50108-Griddy-Unity-Grid-Toolkit-(Free-Open-Source-Incomplete)

I think you will find the project as-is to be perfect for what you are doing.

Regards,
Rob

Well its not exactly what im doing from a quick glance, but I will take a look at it. My setup is simply an array of boxes floating in mid-air so far. But perhaps that project has some useful stuff in it.

Thanks