I’m scripting a ‘noclip’ FPS camera controller that can roam/fly freely anywhere without gravity or physics. Sofar the [W]AD control works well with the mouse-looking control to fly towards anywhere the camera is facing, and W[A]S[D] input takes care of strafing perfectly.
The trouble is with one more feature I wanted to add: mouse wheel for climbing and descending vertically (think of a helicopter). Similar to the WASD mouse look movements, i’d need the scrolling up 'n down to be smoothed.
The Input Manager docs say That the ‘Gravity’ (recentering) value is “…Only used when the Type is key / mouse button”, thus excluding mouse wheel.
This is quite apparent in my game; when I scroll up/down, the vertical movements do work but its terribly choppy/jerky/skippy. I need it to be smoothed like the WASD.
What are the exact calculations needed to implement the lerping / timing / accelerating / deccelerating / etc for this to work? The effect i’m looking for is; when the mouse scrolls (instead of jumping) gradually accelerate towards the target direction. When mouse stops scrolling, gradually deccelerate the latest vertical movement until full stop.
Any help would be greatly appreciated, Thanks
kingIZZZY
Hey,
after our chat I went to see which program on my computer actually had smooth mouse wheeling.
Turns out, the only one was Opera, and even that one was a little jumpy. So since this is kind of something special and might be of use for me later, I did my best and came up with this:
using UnityEngine;
using System.Collections;
public class SmoothMouseWheelTranslation : MonoBehaviour {
float timer;
float translation;
float position;
float target;
float falloff;
float input;
// Increases the target translation
public float speed = 100.0f;
// Maximum acceleration possible by scrolling the wheel faster
public float maxAcceleration = 2.0f;
// How quickly to follow the target
public float followSpeed = 10.0f;
// Across which Vector to translate (Default is Y axis)
public Vector3 translationVector = new Vector3(0, 1, 0);
// Whether or not to use the scrollwheel acceleration
public bool scrollWheelAcceleration = true;
void Update () {
timer+=Time.deltaTime;
input=Input.GetAxis("Mouse ScrollWheel");
// This is the acceleration according to the time difference between the "clicks" of the mousewheel
// If you leave that out, it will be more like Opera scrolling (larger discrete steps but smooth follow)
// The "300" could be adjusted (lower means larger steps, stronger acceleration)
if (scrollWheelAcceleration)
{
if (input != 0)
{
target += Mathf.Clamp((input / (timer * 300)) * speed, maxAcceleration * -1, maxAcceleration);
timer = 0;
}
}
else // Opera-Style
{
target += Mathf.Clamp(input * speed, maxAcceleration * -1, maxAcceleration);
}
// As a falloff we use the distance between position and target
// results in faster Movement at higher distances
falloff = Mathf.Abs(position-target);
// Determine the amount of translation for this frame
translation = Time.deltaTime * falloff * followSpeed;
// 0.001 is our deadzone
if (position+0.001 < target)
{
this.GetComponent<Transform>().Translate(translationVector*translation*-1);
position += translation;
}
if (position-0.001 > target)
{
this.GetComponent<Transform>().Translate(translationVector*translation);
position -= translation;
}
}
}
Play around with it and tell me what you think.
Spot on man, you’re a genious.
Only problem - I have little to no clue what anything does
especially those math operations. All I know is - it works! Awesomely!
I do have a faint clue of the general idea and the comments do help, but for the most part I can’t explain whats happening. So it’s hard to tweak around with everything I’d want…
How do I reverse the direction of the mouse wheel effects? Currently, scroll-fwd goes downward and scroll-bwd goes upward. I’d like that in reverse.
I’ll try to post my JavaScript rendition of this here when I’m done. Can’t thank you enough!
Simple Stuff ![]()
To invert the direction, just change the Translation Vector to (0,-1,0). I admit it should probably default to the other way around.