Flip Normals of a Mesh in Unity

 In Unity, there are a number of primitive object types that can be created directly such as sphere or cube. With these primitive objects, they are made to be visible only from outide the mesh, but what if, for example, you want to make a sphere or a cube visible from inside?

Here is a script to attach to your mesh in order to flip the normals and make it visible from inside:

using System.Linq;
using UnityEngine;
public class FlipNormals : MonoBehaviour
	void Start()
		var mesh = (transform.GetComponent("MeshFilter") as MeshFilter).mesh;
		mesh.uv = mesh.uv.Select(o => new Vector2(1 - o.x, o.y)).ToArray();
		mesh.triangles = mesh.triangles.Reverse().ToArray();
		mesh.normals = mesh.normals.Select(o -> -o).ToArray();

In the screenshot below, the left sphere is the original primitive object: mesh is only visible from the outside. However, on the right sphere, the FlipNormals script is attached: the exterior part of the mesh became invisible. We can only see now the interior part of the sphere.

Left: Original Normals - Right: Flipped Normals

Left: Original Normals – Right: Flipped Normals


Use Simple IK on Unity

In this article, we will show you step by step how to add some inverse kinematics to your character thanks to our asset Simple IK available on the Unity Asset Store.

First of all, if you don’t know the meaning of inverse kinematics, here is the definition:

Inverse kinematics simplifies the process of animating jointed / segmented figures by making the motion of each part related to the motion of the linked parts. That way you simply have to animate the starting and ending joins, and the ones in between will adjust themselves according to the physics programming and create more natural looking movement. In forward kinematics, each piece would have to be animated separately.

Source: http://animation.about.com/od/glossaryofterms/g/def_inversekine.htm


Simple IK is a very handy script that allows you to add inverse kinematics to  some bones and joints. After downloading the Simple IK asset, the script is available inside the folder Takohi with the name SimpleIKSolver.cs.


The aim of this article is to add inverse kinematics to an arm of  a rigged humanoid model. The model we use can be downloaded for free here.
Our goal is to make the avatar touching a sphere with his right hand by always keeping a realistic human posture.

Preparing the scene

First, import the avatar into the scene, then create a empty game object you call ‘Right Arm IK’. This game object can be located anywhere and will be used to hold the script.

Now we need to make a target. A target (any game object) will represent the location the inverse kinematics system will to try to reach. As we said before, we want our avatar to touch with his right arm a sphere, so add a new sphere into the scene and name it ‘Target’. Resize it to have approximately the size of a tennis ball and put it front of our character.


Configuring the IK

Now we will add the inverse kinematics to the right arm of the avatar. From the Takohi folder, drag’n drop the SimpleIKSolver.cs script on the ‘Right Arm IK’ game object.


Here is a description of all parameters:

  • Is Active: enable IK solver
  • Target: transform object to reach
  • Join Entities: list of all joins composing the IK
  • Join Entity:
    • Joint: transform object of the joint
    • Angle Restriction Range:
      • X Axis, Y Axis, Z Axis: enable angle restriction on x/y/z axis
      • X Min, Y Min, Z Min: minimum angle for x/y/z axis (from -180°)
      • X Max, Y Max, Z Max: maximum angle for x/y/z axis (up to 180°)
  • Is Damping: enable damping
  • Damping Max: damping value

For the target, just drag and drop the sphere we called ‘Target’ into the Target property.

Next, we will assign all the joints composing the IK. If you look at the structure of the avatar by dropping down the list, you should find that the right arm has three joints: arm, forearm, and the hand.


In the inspector of ‘Right Arm IK’ game object, set the size of the Join Entities array to 3 and add consecutively these joints. Then enable damping and set a value to 0.05 in order to smooth the movement.


Everything is ready! Now play the scene and you should see the character trying to reach the target with his right hand.


Angles Restrictions

As the picture below, according to the position of the sphere, the arm of the character will have an unrealistic posture that is not possible for a human. We would like to avoid this situation and make our avatar more realistic.


To avoid this kind of unrealistic movement, we will add angles restrictions: the same angles restrictions as a human arm. If you look at the arm below with the axis, you can try by yourself to know with your own arm  and for each axis what is the angles restrictions imposed by your body.


After measuring with my own arm the angles limits for each axis and each joint, these are the values I set as angles restrictions to have a realistic movement of the arm:


By example for the right forearm joint, it can only rotate now from -20° to 20° around the x-axis, from -150° to 0° to the y-axis, and no rotation allowed on the z-axis.

Now play again the scene and move the sphere to different positions. The arm of your avatar will always keep a realistic posture.


Now you have your character that can move his right arm to reach any object on the scene. Of course, inverse kinematics are not only useful for moving arms and legs but can be used in many cases (crane, snake, …) and so our asset Simple IK. Just use your imagination!

If you have any question, please ask us here on the dedicated thread on the Unity forum. We will reply to you as quick as possible.


Virtual Keyboard with Unity

What happens if your application is targeting a touchscreen computer without a physical keyboard but requires keyboard input from the user? One solution, like in many games, is to create a custom virtual keyboard inside the application. But what if you want regional and language support like japanese, chinese, arabic…?
Based from the article from Intel “Touch keyboard access for Windows 8 desktop apps“, the following script will allow you how to call the built-in keyboards provided with the Windows OS.

However, in order to work, the application needs to run in window mode. Please follow this post in order to launch a maximized borderless Unity application for raising the virtual keyboard without disturbing the game.

using UnityEngine;
using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;

public static class VirtualKeyboard {
	static extern IntPtr FindWindow(String sClassName, String sAppName);
	static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
	private static Process _onScreenKeyboardProcess = null;
	/// <summary>
	/// Show the touch keyboard (tabtip.exe).
	/// </summary>
	public static void ShowTouchKeyboard() {
		//ExternalCall("C:\\Program Files\\Common Files\\Microsoft Shared\\ink\\tabtip.exe", null, false);
		ExternalCall("TABTIP", null, false);
	/// <summary>
	/// Hide the touch keyboard (tabtip.exe).
	/// </summary>
	public static void HideTouchKeyboard() {
		uint WM_SYSCOMMAND = 274;
		int SC_CLOSE   = 61536;
		IntPtr ptr = FindWindow("IPTip_Main_Window", null);
		PostMessage(ptr , WM_SYSCOMMAND, SC_CLOSE, 0);
	/// <summary>
	/// Show the on screen keyboard (osk.exe).
	/// </summary>
	public static void ShowOnScreenKeyboard() {
		//ExternalCall("C:\\Windows\\system32\\osk.exe", null, false);
		if(_onScreenKeyboardProcess == null || _onScreenKeyboardProcess.HasExited)
			_onScreenKeyboardProcess = ExternalCall("OSK", null, false);
	/// <summary>
	/// Hide the on screen keyboard (osk.exe).
	/// </summary>
	public static void HideOnScreenKeyboard() {
		if(_onScreenKeyboardProcess != null && !_onScreenKeyboardProcess.HasExited)
	/// <summary>
	/// Set size and location of the OSK.exe keyboard, via registry changes.  Messy, but only known method.
	/// </summary>
	/// <param name='rect'>
	/// Rect.
	/// </param>
	public static void RepositionOnScreenKeyboard(Rect rect)
		ExternalCall("REG", @"ADD HKCU\Software\Microsoft\Osk /v WindowLeft /t REG_DWORD /d " + (int) rect.x + " /f", true);
		ExternalCall("REG", @"ADD HKCU\Software\Microsoft\Osk /v WindowTop /t REG_DWORD /d " + (int) rect.y + " /f", true);
		ExternalCall("REG", @"ADD HKCU\Software\Microsoft\Osk /v WindowWidth /t REG_DWORD /d " + (int) rect.width + " /f", true);
		ExternalCall("REG", @"ADD HKCU\Software\Microsoft\Osk /v WindowHeight /t REG_DWORD /d " + (int) rect.height + " /f", true);
	private static Process ExternalCall(string filename, string arguments, bool hideWindow) {
		ProcessStartInfo startInfo = new ProcessStartInfo();
		startInfo.FileName = filename;
		startInfo.Arguments = arguments;
		// if just command, we don't want to see the console displayed
		if(hideWindow) {
			startInfo.RedirectStandardOutput = true;
	        startInfo.RedirectStandardError = true;
	        startInfo.UseShellExecute = false;
	        startInfo.CreateNoWindow = true;
		Process process = new Process();
		process.StartInfo = startInfo;
		return process;

Maximized borderless Unity application

Sometimes it can be necessary to start your Unity application as a maximized borderless window: one advantage is that it will allow you to start other applications above your game.

One situation is, by example, you want to allow the user of a touchscreen computer to write any text inside your game by launching a virtual keyboard app. In this case, using a borderless full-screen window is that raising the keyboard window does not disturb then game but will still continue running in the background to process keyboard input, and will regain focus automatically as soon as the keyboard is dismissed (see this link).

The following script, by being attached to a game object, will allow you to make your application borderless. Moreover, you can also set it to be fullscreen/maximized or at a defined size.

This script only works for a standalone windows application.

using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using UnityEngine;

/// <summary>
/// Allow a standalone windows application to run without border at a specific screen position or in fullscreen.
/// You can also use the command argument -popupwindow in order to do the same thing with less control.
/// This allows you to run other program over your unity application (as by example a virtual keyboard).
/// </summary>
public class BorderlessMode : MonoBehaviour
	public Rect ScreenPosition;
	public bool IsFullscreen = false;
	static extern IntPtr SetWindowLong (IntPtr hwnd, int  _nIndex, int  dwNewLong);

	static extern bool SetWindowPos (IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

	static extern IntPtr GetForegroundWindow ();
	const uint SWP_SHOWWINDOW = 0x0040;
	const int GWL_STYLE = -16;
	const int WS_BORDER = 1;
	void Start ()
			ScreenPosition = GetFullscreenResolution();
		SetWindowLong (GetForegroundWindow (), GWL_STYLE, WS_BORDER);
		bool result = SetWindowPos (GetForegroundWindow (), 0, (int)ScreenPosition.x, (int)ScreenPosition.y, (int)ScreenPosition.width, (int)ScreenPosition.height, SWP_SHOWWINDOW);
	void Update() {
			ScreenPosition = GetFullscreenResolution();

	Rect GetFullscreenResolution() {
		Resolution resolution = Screen.currentResolution;
		return new Rect(0f, 0f, (float) resolution.width, (float) resolution.height);