SmoothMouseLook
From Unify Community Wiki
(Difference between revisions)
(→Description) |
Tony Lovell (Talk | contribs) m (fixed it for modern Unity, re: rigidbody) |
||
(5 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
[[Category: Camera]] | [[Category: Camera]] | ||
− | [[Category: | + | [[Category: C Sharp]] |
[[Category: MouseLook]] | [[Category: MouseLook]] | ||
Author: asteins | Author: asteins | ||
Line 7: | Line 7: | ||
Modified version of Standard Assets MouseLook.cs, it moves the camera with an easing effect. I basically applied a solution found at [http://forum.unity3d.com/viewtopic.php?p=401679 Unity Community Scripting Forum] to the original C# script. | Modified version of Standard Assets MouseLook.cs, it moves the camera with an easing effect. I basically applied a solution found at [http://forum.unity3d.com/viewtopic.php?p=401679 Unity Community Scripting Forum] to the original C# script. | ||
− | <csharp>using UnityEngine; | + | <syntaxhighlight lang="csharp">using UnityEngine; |
using System.Collections; | using System.Collections; | ||
using System.Collections.Generic; | using System.Collections.Generic; | ||
Line 121: | Line 121: | ||
void Start () | void Start () | ||
− | { | + | { |
− | if ( | + | Rigidbody rb = GetComponent<Rigidbody>(); |
− | + | if (rb) | |
+ | rb.freezeRotation = true; | ||
originalRotation = transform.localRotation; | originalRotation = transform.localRotation; | ||
} | } | ||
Line 140: | Line 141: | ||
return Mathf.Clamp (angle, min, max); | return Mathf.Clamp (angle, min, max); | ||
} | } | ||
− | }</ | + | }</syntaxhighlight> |
Latest revision as of 19:10, 4 February 2016
Author: asteins
[edit] Description
Modified version of Standard Assets MouseLook.cs, it moves the camera with an easing effect. I basically applied a solution found at Unity Community Scripting Forum to the original C# script.
using UnityEngine; using System.Collections; using System.Collections.Generic; [AddComponentMenu("Camera-Control/Smooth Mouse Look")] public class SmoothMouseLook : 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 (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<Rigidbody>(); 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); } }