Create script " SmoothMouseLookClick"

Hello everybody!

First of all, I don't know anything about Scripting, I only use part of script find here and there.

I'm just trying to create a script to look only when the left button of the mouse is pressed. And also try to reduce the "micro freeze" on the mouse move.

For now I succeed in moving the head on axis X and Y when I press the mouse left button. But the issue is that my Y axis is always "activated" and I can move along this axis without pressing the mouse button.

If anyone can help me on this, it would be great.

here is my code:

using UnityEngine;

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

[AddComponentMenu("Camera-Control/Smooth Mouse Look")] public class SmoothMouseLookClick : 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<float> rotArrayX = new List<float>();
float rotAverageX = 0F; 

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

public float frameCounter = 20;

Quaternion originalRotation;

void Update ()
{
    if (Input.GetMouseButton(0))
    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*;*
 *}*
 *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*;*_
 _*}*_
 _*rotAverageX /= rotArrayX.Count;*_
 _*rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX);*_
 _*Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);*_
 <em>_transform.localRotation = originalRotation * xQuaternion;_</em> 
 _*}*_
 _*else*_
 _*{*_ 
 _*rotAverageY = 0f;*_
 <em>_rotationY += Input.GetAxis("Mouse Y") * sensitivityY;_</em>
 _*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);*_
 <em>_transform.localRotation = originalRotation * yQuaternion;_</em>
 _*}*_
_*}*_
_*void Start ()*_
_*{*_ 
 _*if (rigidbody)*_
 _*rigidbody.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);*_
_*}*_
_*```*_
_*<p>}</p>*_

You want to have { } around everything after if (Input.GetMouseButton(0)) to the end of that function. Otherwise, the else statements are being attached to the first if, not the second. Like so:

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

[AddComponentMenu("Camera-Control/Smooth Mouse Look")] 

public class SmoothMouseLookClick : 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<float> rotArrayX = new List<float>();
    float rotAverageX = 0F; 

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

    public float frameCounter = 20;

    Quaternion originalRotation;

    void Update ()
    {
        if (Input.GetMouseButton(0))
        {
            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*;*
 *}*
 *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*;*_
 _*}*_
 _*rotAverageX /= rotArrayX.Count;*_
 _*rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX);*_
 _*Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);*_
 <em>_transform.localRotation = originalRotation * xQuaternion;_</em> 
 _*}*_
 _*else*_
 _*{*_ 
 _*rotAverageY = 0f;*_
 <em>_rotationY += Input.GetAxis("Mouse Y") * sensitivityY;_</em>
 _*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);*_
 <em>_transform.localRotation = originalRotation * yQuaternion;_</em>
 _*}*_
 _*}*_
 _*}*_
 _*void Start ()*_
 _*{*_ 
 _*if (rigidbody)*_
 _*rigidbody.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);*_
 _*}*_
_*}*_
_*```*_