Skip to content

Instantly share code, notes, and snippets.

@keiranlovett
Created August 18, 2015 07:48
Show Gist options
  • Save keiranlovett/90e0f65a737a42f1c38d to your computer and use it in GitHub Desktop.
Save keiranlovett/90e0f65a737a42f1c38d to your computer and use it in GitHub Desktop.
NoClipMouseLook
using UnityEngine;
using System.Collections;
public class NoClipMouseLook : MonoBehaviour {
public float sensitivityX = 8F;
public float sensitivityY = 8F;
float mHdg = 0F;
float mPitch = 0F;
void Start()
{
// owt?
}
void Update()
{
if (!(Input.GetMouseButton(0) || Input.GetMouseButton(1)))
return;
float deltaX = Input.GetAxis("Mouse X") * sensitivityX;
float deltaY = Input.GetAxis("Mouse Y") * sensitivityY;
if (Input.GetMouseButton(0) && Input.GetMouseButton(1))
{
Strafe(deltaX);
ChangeHeight(deltaY);
}
else
{
if (Input.GetMouseButton(0))
{
MoveForwards(deltaY);
ChangeHeading(deltaX);
}
else if (Input.GetMouseButton(1))
{
ChangeHeading(deltaX);
ChangePitch(-deltaY);
}
}
}
void MoveForwards(float aVal)
{
Vector3 fwd = transform.forward;
fwd.y = 0;
fwd.Normalize();
transform.position += aVal * fwd;
}
void Strafe(float aVal)
{
transform.position += aVal * transform.right;
}
void ChangeHeight(float aVal)
{
transform.position += aVal * Vector3.up;
}
void ChangeHeading(float aVal)
{
mHdg += aVal;
WrapAngle(ref mHdg);
transform.localEulerAngles = new Vector3(mPitch, mHdg, 0);
}
void ChangePitch(float aVal)
{
mPitch += aVal;
WrapAngle(ref mPitch);
transform.localEulerAngles = new Vector3(mPitch, mHdg, 0);
}
public static void WrapAngle(ref float angle)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment