Forum rules - please read before posting.

Any way to integrate this Mouse Look Smoothing script into AC?

I've been using a MouseLook script I found suggested in these forums for the First Person sections of my game to make the first person mouse movements smooth.

The smoothing in this works perfectly, but for some reason the right joystick doesn't work for looking around (but the joystick works for all other inputs)

When I disable this script and just enable AC's FirstPersonCamera, both mouse and joystick work correctly, but the default AC mouse look is extremely jittery and not smooth.

Here's a video comparing the smoothness of movements of both AC's movements and the MouseLook script's movements.

So to make both mouse and joystick work with smooth movements, my options are to either
1. Implement the smoothing from the MouseLook script into the AC script that controls First Person mouse look movements and use only AC with this smoothing added, or
2. Use the MouseLook script only, but add some way of joystick control into that script.

Note that I'm using ReWired, and in order to ensure compatibility of all joysticks (Dualshock/ Xbox controllers) I have to remove the CursorHorizontal/ Vertical or JoyX/JoyY etc inputs from the Unity Input Manager. Because say if I add a Joy X entry into the Input Manager and specify the axis for it (4th axis etc) it then conflicts for different controllers - For Xbox I think that stands for the right stick, and for Dualshock that is the Left Trigger negative. So if someone connects the Dualshock, instead of it being activated by the right stick, it'll infact be always activated since the Left Trigger is always negative (not pressed) This leads to the character spinning. For these reasons I can't add a Joy X etc entry to the input manager as a possible solution.

Here is the MouseLook script. Is there any way to implement the "FramesofSmoothing" from this into AC? Thanks! :)

    // original by asteins
    // adapted by @torahhorse
    // http://wiki.unity3d.com/index.php/SmoothMouseLook

    // Instructions:
    // There should be one MouseLook script on the Player itself, and another on the camera
    // player's MouseLook should use MouseX, camera's MouseLook should use MouseY

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

    public class MouseLook : MonoBehaviour
    {

        public enum RotationAxes { MouseX = 1, MouseY = 2 }
        public RotationAxes axes = RotationAxes.MouseX;
        public bool invertY = false;

        public float sensitivityX = 10F;
        public float sensitivityY = 9F;

        public float minimumX = -360F;
        public float maximumX = 360F;

        public float minimumY = -85F;
        public float maximumY = 85F;

        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 framesOfSmoothing = 5;

        Quaternion originalRotation;

        void Start ()
        {           
            if (GetComponent<Rigidbody>())
            {
                GetComponent<Rigidbody>().freezeRotation = true;
            }

            originalRotation = transform.localRotation;
        }

        void Update ()
        {


            if (axes == RotationAxes.MouseX)
            {           
                rotAverageX = 0f;

                rotationX += Input.GetAxis("MouseX") * sensitivityX * Time.timeScale;

                rotArrayX.Add(rotationX);

                if (rotArrayX.Count >= framesOfSmoothing)
                {
                    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;

                float invertFlag = 1f;
                if( invertY )
                {
                    invertFlag = -1f;
                }
                rotationY += Input.GetAxis("MouseY") * sensitivityY * invertFlag * Time.timeScale;

                rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

                rotArrayY.Add(rotationY);

                if (rotArrayY.Count >= framesOfSmoothing)
                {
                    rotArrayY.RemoveAt(0);
                }
                for(int j = 0; j < rotArrayY.Count; j++)
                {
                    rotAverageY += rotArrayY[j];
                }
                rotAverageY /= rotArrayY.Count;

                Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
                transform.localRotation = originalRotation * yQuaternion;
            }
        }

        public void SetSensitivity(float s)
        {
            sensitivityX = s;
            sensitivityY = s;
        }

        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);
        }
    }

Comments

  • The script references "MouseX" and "MouseY" inputs - you should just have to define these mapped to a joystick to get them to read that.

    But the built-in AC first person shouldn't be so stiff. What's your AC version?

    Rewired only adds another layer of complexity. If you're looking to improve the built-in first person motion, remove it temporarily and bring back your axes in the Input Manager - then let's see their values, as well as that of the FirstPersonCamera Inspector and Settings Manager.

  • edited May 2020

    AC version is 1.70.2

    The script references "MouseX" and "MouseY" inputs - you should just have to define these mapped to a joystick to get them to read that.

    Did you mean define MouseX and MouseY in the input manager for joystick control? The issue with that like I mentioned is that I have to define which axis it controls, and these conflict for different controllers. So yes, that does work in that it enables joystick control for mouse look, but connecting a dualshock makes the camera spin.

    My settings are:

    Free aim acceleration: 50
    Max free aim speed: 5

    FirstPersonCamera Freelook sensitivity : 10, 10

    After adding these back in to the inputs -
    CursorHorizontal and CursorVertical
    Gravity - 1000
    Dead - 0.01
    Sensitivity - 0.02

    Update:

    After disabling Rewired, it does seem to be working smoothly. Re-enabling Rewired caused the same issues.

    I then lowered the Mouse sensitivity within Rewired, and now the jitter seems to be almost fully gone as well, which was unexpected. Maybe the high sensitivity in Rewired was grappling with the lower values in AC.

    So far so good, I'll let you know if the jitter issues return. I struggled with this issue for days but somehow didn't attempt this solution! Thanks for the help.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.