Mouse Look C# script Unity 5

I have used this Mouse Look C# script and attached to the main camera in my game (see code below).
Then to get this Mouse Look behavior to work on my Game Controllers Right Joystick this is what I did.

Go to Edit >Project Settings > Input

mouselook script for joystick

 

You should see a list like this one below appear in the Inspector Panel. (Note: you wont see Stick X and Stick Y. I have already went through this process so the list below is not exactly what you will see)

To make this MouseLook script work on your Game Controller Right stick you need to right-click on Mouse X and Mouse Y and select “Duplicate”.

mouselook-script-unity-02

You can then rename the copied Inputs to something like Stick X and Stick Y. Then set the Type to Joystick Axis and select 4th axis for the X and 5th Axis for the Y.

mouselook-script-unity-03

 

MouseLook script in C# for Unity


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

[AddComponentMenu(“Camera-Control/Smooth Mouse Look”)]
public class mouseLook : MonoBehaviour {

public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;

public float minimumX = -360F;
public float maximumX = 360F;

public float minimumY = -60F;
public float maximumY = 60F;

float rotationX = 0F;
float rotationY = 0F;

private List rotArrayX = new List();
float rotAverageX = 0F;

private List rotArrayY = new List();
float rotAverageY = 0F;

public float frameCounter = 20;

Quaternion originalRotation;

void Update ()
{
if (axes == RotationAxes.MouseXAndY)
{
rotAverageY = 0f;
rotAverageX = 0f;

rotationY += Input.GetAxis(“Mouse Y”) * sensitivityY;
rotationX += Input.GetAxis(“Mouse X”) * sensitivityX;

rotArrayY.Add(rotationY);
rotArrayX.Add(rotationX);

if (rotArrayY.Count >= frameCounter) {
rotArrayY.RemoveAt(0);
}
if (rotArrayX.Count >= frameCounter) {
rotArrayX.RemoveAt(0);
}

for(int j = 0; j < rotArrayY.Count; j++) {
rotAverageY += rotArrayY[j];
}
for(int i = 0; i < rotArrayX.Count; i++) { rotAverageX += rotArrayX[i]; } rotAverageY /= rotArrayY.Count; rotAverageX /= rotArrayX.Count; rotAverageY = ClampAngle (rotAverageY, minimumY, maximumY); rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX); Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left); Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up); transform.localRotation = originalRotation * xQuaternion * yQuaternion; } else if (axes == RotationAxes.MouseX) { rotAverageX = 0f; rotationX += Input.GetAxis(“Mouse X”) * sensitivityX; rotArrayX.Add(rotationX); if (rotArrayX.Count >= frameCounter) {
rotArrayX.RemoveAt(0);
}
for(int i = 0; i < rotArrayX.Count; i++) { rotAverageX += rotArrayX[i]; } rotAverageX /= rotArrayX.Count; rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX); Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up); transform.localRotation = originalRotation * xQuaternion; } else { rotAverageY = 0f; rotationY += Input.GetAxis(“Mouse Y”) * sensitivityY; rotArrayY.Add(rotationY); if (rotArrayY.Count >= frameCounter) {
rotArrayY.RemoveAt(0);
}
for(int j = 0; j < rotArrayY.Count; j++) {
rotAverageY += rotArrayY[j];
}
rotAverageY /= rotArrayY.Count;

rotAverageY = ClampAngle (rotAverageY, minimumY, maximumY);

Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
transform.localRotation = originalRotation * yQuaternion;
}
}

void Start ()
{
Rigidbody rb = GetComponent();
if (rb)
rb.freezeRotation = true;
originalRotation = transform.localRotation;
}

public static float ClampAngle (float angle, float min, float max)
{
angle = angle % 360;
if ((angle >= -360F) && (angle <= 360F)) {
if (angle < -360F) { angle += 360F; } if (angle > 360F) {
angle -= 360F;
}
}
return Mathf.Clamp (angle, min, max);
}
}

Pick up and Destroy Unity 5 Object

Use this code when you want your character to pick up an object e.g. Coins. Create a empty JavaScript file in Unity 5 and add the following line of code. Then drag and drop the JavaScript file onto any object you want to destroy when you touch it with a collider. The collider must have Is Trigger switched on.

function OnTriggerEnter (info : Collider) {
Destroy(gameObject);
}

Simple Coin Spin Script for Unity 5

Add this simple Coin Spin Script to any Object in Unity 5 to make it rotate.

Create a new blank JavaScript file and paste the following code into it. Then just drag the JavaScript file onto any object you want to make spin.

function Update () {
transform.Rotate(0, 2, 0, Space.World);
}

Here is the C# script version for Unity 5

using UnityEngine;
using System.Collections;

public class coinSpin : MonoBehaviour
{
public float spinSpeed = 10f;
void Update ()
{
transform.Rotate(Vector3.up, spinSpeed * Time.deltaTime);
}

}