Unity Fails to Build iOS Game due to “LocationService” being used

Ran into an interesting problem while trying to compile a Unity project for iOS. It failed to build because

“LocationService class is used but Locations Usage Description is empty. App will not work on iOS 10+.
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)”

LocationService class is used but Locations Usage Description is empty

Well as far as I know there is not anything calling or requesting location services. I probably could have just put a reason for requesting the service under the build options in Project Settings. But why “use” something we don’t need?

Using the handy grep tool from a terminal, I was able to search through the project for “LocationService” and that returned a couple hits for files in the PlayMaker folder.

Looks like PlayMaker has a couple preconfigured “scripts” for Starting, Stopping, and Requesting Location.

Deleting the following four C# files resolved the error and it built fine afterwards.

GetLocationInfo
StopLocationServiceUpdates
StartLocationServiceUpdates
ProjectLocationToMap

You can find these files in your project under Assets > PlayMaker > Actions > Device

Unity Android Build – Required API Level 30

Android SDK missing required platform API

Ran into the above issue while trying to build a Unity project. Android studio had the correct version installed, why was it not able to use API level 30?

Ok So hit Update Android SDK. Unity Launches a PowerShell window and tries to upgrade the Android SDK level, but fails with

“Unable to install additional SDK platform. Please run the SDK Manager manually to make sure you have the latest set of tools and the required platforms installed.”

Looks like there was a bug with the space in the path to the command Unity was running.

https://forum.unity.com/threads/android-sdk-29-unable-to-install-additional-sdk-platform-issue-workaround.963626/

Looks like there are a couple of work arounds for this. I had Android Studio installed so I opened up that folder and copied that platform version to my unity folder.

Copy Android platform from Android Studio to Unity Folder

Android Studio folder should be in AppData\Local\Android\Sdk\platforms

Unity Project folder should be “C:\Program Files\Unity\Hub\Editor\2020.3.19f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\platforms”

You should now be able to build the project in Unity now with the appropriate API version.

Unity – Rotate Player by Dragging Finger on Screen

C# Code for rotating a player in Unity by swiping on the screen.

This is actually really simple to do. Create a script called PlayerRotation or something, then put in the following code.

 
 public float speed;
 void Update()
    {
        // Touch controls for rotation
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
        {
            Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
            // Rotate Player.  Could change Rotate to Translate to move the player.
            transform.Rotate(0, touchDeltaPosition.x * speed, 0);
   

    }

Assign the Script to the player and specify the speed in the Unity Inspector. The public float speed variable will increase or decrease how fast you rotate around. You can also set it to a negative number to rotate the opposite direction you drag with your finger.

Unity speed of rotation for player.

Unity Hub Failing to Launch Unity Project

While trying to launch a project in Unity I kept getting met with, nothing… Click on the project the Hub disappears and nothing happens… Tried uninstalling and reinstalling both Unity and the Unity hub.

I did receive an error similar to the following when trying to launch Unity directly.

windows\system32\msvcp120.dll is either not designed to run on windows or it contains an error

Doing some digging online shows that it has to do with the Visual C++ Redistributable runtime package which can be downloaded here

https://www.microsoft.com/en-us/download/details.aspx?id=40784

Unity was back to working normal after installing that.

More information is available at the following link.

https://forum.unity.com/threads/unity-exe-bad-image.500938/

Clamp rotation of camera in Unity

Some notes and issues when trying to clamp the rotation of a camera in Unity. All the code was put on a script assigned to a controller that controlled the camera view. Used the Simple Touch Controller from the asset store.

Problem trying to clamp EulerAngles

It appears that the following code does not work as EulerAngles expects a value from 0-365.

transform.localEulerAngles = new Vector3(Mathf.Clamp(transform.localEulerAngles.x - rightController.GetTouchPosition.y * Time.deltaTime * speedContinuousLook, 0f , 20f ),transform.localEulerAngles.y + rightController.GetTouchPosition.x * Time.deltaTime * speedContinuousLook, 0f);

Extra debug info

Debug.Log("Rotation = " + transform.localEulerAngles.ToString());

Code to clamp rotation

You have to do some math to take the value from 0-365, then translate it to -90 – 90 so it can be clamped properly

// Put these with the rest of your public variables.  These should show up in the Unity Inspector and allow you to modify the values
public float speedProgressiveLook;
public float clampXRotationUp;
//  The following lines allow the script to Clamp the camera X rotation and should go in the Update function

float playerRotation = transform.localEulerAngles.x - rightController.GetTouchPosition.y * Time.deltaTime * speedContinuousLook; 

if (playerRotation > 180)        {             
    playerRotation = playerRotation - 360 ;  // Sets our working numbers between -180 - 180  
}    
     
playerRotation = Mathf.Clamp(playerRotation, clampXRotationDown, clampXRotationUp);

Vector3 newRotation;
newRotation.x = playerRotation;
newRotation.y = transform.localEulerAngles.y + rightController.GetTouchPosition.x * Time.deltaTime * speedContinuousLook;
newRotation.z = 0;

transform.localEulerAngles = newRotation;

The following links were helpful.

https://gamedev.stackexchange.com/questions/120500/using-mathf-clamp-in-vector3-to-restrict-object-drag

https://www.reddit.com/r/Unity3D/comments/8ue9fp/mathfclamp_is_refusing_to_work_can_anybody_offer/

https://stackoverflow.com/questions/26682155/using-mathf-clamp-in-unity-for-boundries

PlayMaker reverting to local variables instead of using global variable

In PlayMaker you can experience issues with variables if you have a Global variable (Green in picture) and a Local variable (Red in picture) that have the same name. PlayMaker will let you select the Global variable however, you’ll get erratic behavior and it seems to revert to the local variable. Work around? Rename one of the variables.

Unity not loading scene levels

Problem: Game would finish level, but would not load next one.

The issue was that all the levels were not included in the build scene, (File -> Build Settings -> Scenes In Build) so even though the level was correct in PlayMaker, it would not load the level in the Game window while testing.

To add scene to build, open the scene up in the editor, then open the Build Settings from File -> Build Settings… or Ctrl+Shift+B and Add Open Scenes

Unity Button can’t access script functions

When you have a button object there is the On Click () area that can execute different actions like a PlayMaker script or a function in a C# script.

Issue with the C# script. You can’t drag and drop the script from the Assets Panel. The script needs to be assigned to an object, then drag and drop that object on the On Click () object. Then select the script and then the function from the drop down there.

Example:

Code snippet below is the EnableDisableAudio function that gets executed by the On Click () function above. Note that the script has to be assigned to an object, in this case the button.

    public void EnableDisableAudio()
    {
        counter++;
        if (counter % 2 == 1)
        {
            Debug.Log("ButtonToggled - On");

        }
        else
        {
            Debug.Log("ButtonToggled - Off");
        }
    }

More info

https://gamedev.stackexchange.com/questions/100995/why-is-c-method-not-showing-up-for-buttons-on-click