Blog

Interacting with Objects Unity3D

January 4, 2010 by Devin Reimer

Interactive TV Demo Screenshot

If you missed the first part of this Unity3D series you can read it here.

This example demonstrates how to interact with with objects in Unity.

The mouse cursor was removed and replaced with a cursor in the middle of the screen similar to an FPS. When this cursor is over an object that support interactivity, the cursor changes and the GUI will display a message based on the possible interaction. This is achieved by creating an abstract class called ‘Interactive’ with two abstract functions GetInteractionMessage() and Interact(). All interactive objects will inherit this class and support these two functions.

Within the Update() in the GUI class a Physics.Raycast is done using a ray moving outward from the center of the screen. This returns the first GameObject in which this ray collides. Then it tests to see if this object or a parent of this object has a component of type ‘Interactive’. If type ‘Interactive’ is detected it then can retrieve information about the possible interaction or if the left mouse button is down tell this object to perform an action.

In this example clicking on each TV changes its channel (based on multiple materials) and rotates its dial.

As you may have noticed the TV model has received a significant upgrade. A thanks goes out to Calin Reimer for creating this new model.

Hints and Gotchas:

  • Use an abstract class as the parent class for all interactive objects, this way you won’t have to rewrite the basic interaction code for each object. This method also make detection a lot easier.
  • To create a Ray moving outward from the center of the screen, use the following code: Camera.main.ScreenPointToRay(new Vector3(Screen.width/2,Screen.height/2));
  • Use layers to have certain materials ignore light sources (ie. TV Screen), then use a Self-Illumin shader to brighten this material.
  • If you can get someone else to model for you, do it. It speeds up development :)

To check out the demo click here.

To get the source (unitypackage) click here.

Source Requirements: Unity 2.6, but you will still need Unity Pro to use the Render Texture feature.

9 Responses to "Interacting with Objects Unity3D"

  1. Domeboy says:

    Great work – keep up the Unity posts!

  2. warkarma says:

    Hey. Which version of Unity you are using? Coz I cant unpack it. It crashes. Could you unpack it and RAR it so I can see the code and make it working on my version? Thanks a lot.

  3. Devin Reimer says:

    @warkarma

    I am using Unity 2.6 Pro. To make sure the package didn’t have a problem I tested this package on a completely different machine with Unity 2.6 (Standard version) and it imported correctly. So if you could let me know any error messages you got that would be very helpful.

  4. warkarma says:

    @Devin Reimer

    Hi. I am using Unity 2.5 and it happens if I try to import from a different version of Unity. But dont worry. I got all the stuff now.

    Thanks a lot.

  5. moh says:

    thank you for share , very useful

  6. Richard says:

    Hi,

    Is it possible to get the data from the RenderTexture so that I can send it over the network and load it back up in another scene?

  7. ullu movies says:

    I’ve shared your web site in my social networks

Leave a response