This script is parented to the arm gameobject to make so that when you move your mouse the arms move a little ahead of the camera and then settle back to be aligned with the camera. The problem is that when I move my mouse the arms flip randomly between what I want and being randomly flipped 180 degrees around the Y-axis. This is a modified MouseLook script from standard assets.
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Sway")]
public class MouseSway : MonoBehaviour {
public float sensitivityX = 1F;
public float sensitivityY = 1F;
public float offsetY = 0;
public float minimumX = -30F;
public float maximumX = 30F;
public float minimumY = -30F;
public float maximumY = 30F;
float rotationX = 0F;
float rotationY = 0F;
float tSinceLastMoveX = 0F;
float tSinceLastMoveY = 0F;
Quaternion originalRotation;
void FixedUpdate ()
{
// Read the mouse input axis
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
offsetY = 0;
}
void Start ()
{
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
originalRotation = transform.localRotation;
}
public static float ClampAngle (float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp (angle, min, max);
}
}
the script that is posted is attached to the arms and the camera and character still have the default mouselook script.
– scipiothegreatRight. Well, I must say, that wasn't really obvious. In any case, I think you've got it the wrong way around- the arms need to use the default FP camera controller, and the camera needs to be slaved to them. This way, the arms will always be where the player points them, but the camera can lag behind a little.
– syclamoth