Okay I'm making an FPS shooter type game. awhile ago I found these two mouse look scripts earlier I need help fixing (and this is the first time I've really tried to script in C# opposed to java).One other thing I'd like to do although I am willing to use the the script twice on a child object is to make the script rotate the parent object's x axis while the child objects (put in a variable) y axis but I am willing to do otherwise :
this script works more like I want it to and I can easily modify one of the variables to block off x rotation but I cannot set my own clamp to the y axis
using UnityEngine;
using System.Collections;
// Very simple smooth mouselook modifier for the MainCamera in Unity by FatiguedArtist
//(Francis R. Griffiths-Keam) - www.fatiguedartist.com
[AddComponentMenu("Camera/Simple Smooth Mouse Look ")]
public class SimpleSmoothMouseLook : MonoBehaviour {
public bool lockCursor = false;
public float sensitivity = 30;
public int smoothing = 10;
float ymove;
float xmove;
int iteration = 0;
float xaggregate = 0;
float yaggregate = 0;
//int Ylimit = 0;
public int Xlimit = 20;
void Start()
{
// make the cursor invisible and locked?
if (lockCursor)
{
Screen.lockCursor = true;
}
}
void FixedUpdate () {
// ensure mouseclicks do not effect the screenlock
if (lockCursor)
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
{
Screen.lockCursor = true;
}
float[] x = new float[smoothing];
float[] y = new float[smoothing];
// reset the aggregate move values
xaggregate = 0;
yaggregate = 0;
// receive the mouse inputs
ymove = Input.GetAxis("Mouse Y");
xmove = Input.GetAxis("Mouse X");
// cycle through the float arrays and lop off the oldest value, replacing with the latest
y[iteration % smoothing] = ymove;
x[iteration % smoothing] = xmove;
iteration++;
// determine the aggregates and implement sensitivity
foreach (float xmov in x)
{
xaggregate += xmov;
}
xaggregate = xaggregate / smoothing * sensitivity;
foreach (float ymov in y)
{
yaggregate += ymov;
}
yaggregate = yaggregate / smoothing * sensitivity;
// turn the x start orientation to non-zero for clamp
Vector3 newOrientation = transform.eulerAngles + new Vector3(-yaggregate, xaggregate, 0);
// xclamp = Mathf.Clamp(newOrientation.x, Xlimit, 360-Xlimit)%360;
// rotate the object based on axis input (note the negative y axis)
transform.eulerAngles = newOrientation;
}
}
this script clamping rotation works fine but I can't click any buttons on the mouse or the script won't work any more, and while this script works smoothly it drags a little and does not stop when I no longer move the mouse.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SmoothMouseLook : MonoBehaviour
{
/*
This script is used to average the mouse input over x
amount of frames in order to create a smooth mouselook.
*/
//Mouse look sensitivity
public float sensitivityX = 2f;
public float sensitivityY = 2f;
//Default mouse sensitivity
public float defaultSensX = 2f;
public float defaultSensY = 2f;
//Minimum angle you can look up
public float minimumY = -60f;
public float maximumY = 60f;
//Number of frames to be averaged, used for smoothing mouselook
public int frameCounterX = 35;
public int frameCounterY = 35;
//Mouse rotation input
private float rotationX = 0f;
private float rotationY = 0f;
//Used to calculate the rotation of this object
private Quaternion xQuaternion;
private Quaternion yQuaternion;
private Quaternion originalRotation;
//Array of rotations to be averaged
private List rotArrayX = new List ();
private List rotArrayY = new List ();
void Start ()
{
//Lock/Hide cursor
Screen.lockCursor = true;
if (rigidbody)
rigidbody.freezeRotation = true;
originalRotation = transform.localRotation;
}
void Update ()
{
if (Screen.lockCursor) {
//Mouse/Camera Movement Smoothing:
//Average rotationX for smooth mouselook
float rotAverageX = 0f;
rotationX += Input.GetAxis ("Mouse X") * sensitivityX;
//Add the current rotation to the array, at the last position
rotArrayX.Add (rotationX);
//Reached max number of steps? Remove the oldest rotation from the array
if (rotArrayX.Count >= frameCounterX) {
rotArrayX.RemoveAt (0);
}
//Add all of these rotations together
for (int i_counterX = 0; i_counterX < rotArrayX.Count; i_counterX++) {
//Loop through the array
rotAverageX += rotArrayX[i_counterX];
}
//Now divide by the number of rotations by the number of elements to get the average
rotAverageX /= rotArrayX.Count;
//Average rotationY, same process as above
float rotAverageY = 0;
rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
rotArrayY.Add (rotationY);
if (rotArrayY.Count >= frameCounterY) {
rotArrayY.RemoveAt (0);
}
for (int i_counterY = 0; i_counterY < rotArrayY.Count; i_counterY++) {
rotAverageY += rotArrayY[i_counterY];
}
rotAverageY /= rotArrayY.Count;
//Apply and rotate this object
xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
}
private float ClampAngle (float angle, float min, float max)
{
if (angle < -360f)
angle += 360f;
if (angle > 360f)
angle -= 360f;
return Mathf.Clamp (angle, min, max);
}
}
↧