Another weekend, another Unity hackathon. One of the nice thingies I created today is a simple script that tells me what object my mouse is pointing at.
The source code for Physics.Raycast offers examples that help with mouse-to-screen conversion and ray casting, but they don’t wrap it up into a plug ‘n play script.
This one should be just that.

I wonder if this is the right way to handle this or if there is a built-in function to do it, which I overlooked.

I’ve also attached a screenshot and demo level I’ve dubbed Neon City. It uses self-illuminated cylinders with a black/white texture for the borders. There are stacks of boxes with a brushed steel texture that uses the Reflective/Parallax Diffuse shader for shininess and for a reflection of the sky texture. Either way, it was fun playing around with baking lightmaps and seeing what you can do with Beast. It’s really, really easy to setup a scene that seems to have small lights every everywhere, even though there is only a single Directional light in the entire scene.

Another fun thing Gerard and me discovered today is how easy it is to place a camera in the scene and have it render its output onto a plane somewhere else, to create a security camera + monitor system.

One thing that bugged me about earlier demos is that almost every edge in the game appeared jagged instead of being smoothed by anti-aliasing. In order to render real-time shadows from point lights such as fireballs, the rendering path needs to use Deferred Lighting. The trade-off, as documented on the Unity3D site, is that there is no anti-aliasing in Deferred Lighting mode. The default Forward Rendering mode gives you anti-aliasing, but no real-time shadows from Spot/Point lights. Only from one Directional light.

Unity is very helpful that it lets you know that only directional lights have shadows in forward rendering, but according to Warwick Allison, you can create your own shader with the fullforwardshadows setting to enable shadows on your surface. After an hour or so searching and surfing, I did not manage to find a package with shaders that add this behavior to the default shaders, so I guess I’m looking for something that either people don’t find too bothersome or that’s too hard to do. For now, I hope that a future version of Unity will fix this.

Anyway, here’s the source code for the detector script and below that is a screenshot of demo 7.


#pragma strict;

/***********************************************************************************************

Usage example:

  public var cam : Camera;
  public var text : GUIText;

  // If the mouse points at a an object, display the name of that object on screen.
  function Update () {
    var obj = MousePointerObjectDetector.Detect(cam);
    if (obj) {
      text.text = "Hit: " + obj.name;
    } else {
      text.text = "No hit";
    }
  }

***********************************************************************************************/

// For the camera, return the Transform of the object that is pointed at by the camera.
// Returns null if nothing is pointed at.
class MousePointerObjectDetector {
  public static function Detect(camera : Camera) {
    var pos : Vector3 = Input.mousePosition;
    var ray : Ray = camera.ScreenPointToRay (pos);
    var hit : RaycastHit;
    if (Physics.Raycast (ray.origin, ray.direction, hit)) {
        return hit.collider.transform;
    } else {
      return null;
    }
  }
}

Screenshot of demo 7