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.

Raspberry Pi – Ping IP Address and Toggle LED

The following script is for monitoring if an IP address is reachable or not. If it becomes unavailable the script will turn on a LED that is plugged into one of the GPIO pins of the Raspberry Pi. View pinout here


# Script to ping ip address and turn on LED on if device is unreachable.
                                                                                                                                                                                                 nPin="18"  # Change if GPIO pin is different                                                                                                     
ledPin="gpio${nPin}"                                                                                                                                                                                                                            toPing=""  # Change to address you want to ping

echo "${nPin}" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/${ledPin}/direction

if ( fping -r1 $toPing | grep -v alive ); then
         echo "Internet unreachable"
         # Turn on LED
         echo "1" > /sys/class/gpio/${ledPin}/value
         # Turn off LED 
         echo "0" > /sys/class/gpio/${ledPin}/value

Save script as and make it executable.

chmod +x

and run the script.


Run script in crontab

You can setup the script to run every minute using a crontab

crontab -e

Add the following line

*/1 * * * * /home/pi/

Should now execute the script every minute and not need any human interaction.

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 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.


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()
        if (counter % 2 == 1)
            Debug.Log("ButtonToggled - On");

            Debug.Log("ButtonToggled - Off");

More info

Unity script for enemy to follow player

The following script will let an object follow a player when it is within a certain range and will stop following it once it is out of a certain range

The following variables can be adjusted from the Inspector.

Attack Speed = How fast the game object moves
Attack Distance = How close does the player need to be to start moving
Buffer Distance = How far away from the player should the game object stop
Player = Game player to target

To use script, create a new C# script named FollowPlayer and paste in the following.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowPlayer : MonoBehaviour

    public float attackSpeed = 4;
    public float attackDistance;
    public float bufferDistance;
    public GameObject player;

    Transform playerTransform;

    void GetPlayerTransform()
        if (player != null)
            playerTransform = player.transform;
            Debug.Log("Player not specified in Inspector");

    // Start is called before the first frame update
    void Start()

    // Update is called once per frame
    void Update()
        var distance = Vector3.Distance(playerTransform.position, transform.position);
        // Debug.Log("Distance to Player" + distance);
        if (distance <= attackDistance)
            if (distance >= bufferDistance)
                transform.position += transform.forward * attackSpeed * Time.deltaTime;

Helpful Links

Unity/Playmaker create 1 second loop

The following picture shows the basics for create a loop that runs every second. You’ll need to create a float variable (Named oneSecond in the screen shot) that has a value of 1 for one second. Changed if desired.

Put your actions in the “Execute every 1 Second” state

For more info on Get Time Info, visit