Posted 2 years ago

Unity3D: Using RayCast to detect user touching an object

I am developing a new trial app just to learn a bit of Unity3D. I am therefore facing the basic problems which are quite typical of games development and hope the solutions I find may help you. Yesterday I tried to implement the javascript code detecting whether the user is touching a particular area of the game.  

To this end I have simply compared the Input.mousePosition vector with the desired coordinates. I later found out that, though this approach works, it is not that elegant as one has to  manually check what are the right coordinates which change when you execute the same app on different devices. 

The best approach is instead using the Physics.Raycast static method. 
As you can read in http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html 
this method casts a ray against all the colliders in the scene from an origin point with a certain direction and distance and provides as output the information about the first collider it hit.

So you have to create an empty GameObject (GameObject->Create Empty) which represents the area in the game you want to catch user touches, attach a box collider to it (Component->Physics->Box Collider) and modify the collider width and height according to your needs. This comes very in handy as if you want to adjust the touchable area characteristics you just have to change the object parameters (i.e. position, colliders width/height). After that attach a script to the object and implement the Update function this way:

function Update () {
   if (Input.GetMouseButtonDown(0)){
        var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        var hit:RaycastHit;
             if (Physics.Raycast (ray, hit,100)) {
                 if(hit.collider.name==this.name){
                   //your object has been hit
                 }
             }else{
                   //nope
             }
   }

The ScreenPointToRay method returns a ray going from the caller camera object through a screen point passed as parameter (in our case the point is actually the mouse/touch position). The second parameter is a RaycastHit variable which is the output containing the data about the collider that has been hit by the ray. We just have to check whether the collider is the desired object (this) or not and  perform the desired actions. 


Posted 2 years ago

Unity3D: Dynamic Font Size in GUI Elements

I have just passed a couple of hours trying to dynamically resize label fonts of a Unity3D GUI according to the screen size. I have not found any blog on the internet posting a complete solution this quite easy issue so I decided to help Unity newbies facing the same problem.

First of all you have to create a new GUI skin asset. A GUI skin is basically a set of parameters configuring any relevant aspect (i.e. background, font, font size, etc.) of your GUI elements (i.e. Box, Label, TextField, Button etc.).  To create a GUI skin select the folder you want to contain it and choose the Assets->Create->GUI Skin menu.

Before your OnGUI function (you shall already have one if you are trying to adjust your GUI…) define a GUISkin variable and remember to associate it to your just created asset in the related Inspector variable otherwise you will get a null reference error. 

Within the OnGUI function you only have to set your custom skin to the used one (GUI.skin = customSkin) and change the desired fonts size (box, textField) with a percentage of the current screen height (for instance) as depicted in the following code.

var customSkin : GUISkin; 

function OnGUI () {
    var w = Screen.width;
    var h = Screen.height;
    GUI.skin = customSkin;
    customSkin.box.fontSize = 0.04*h;
    customSkin.textField.fontSize = 0.08*h;

    …

Posted 2 years ago
Posted 2 years ago

SU - Sketches 001

Posted 2 years ago

SU - Sketches 002

Posted 2 years ago

SU - Sketches 003