- Home /
Is it possible to reuse input's acceleration in custom script?
I'd like to implement an RTS-like camera with panning. I'd like the movement to be smooth, and I can do that easily with
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime)
Because GetAxis returns a [0..1] value to simulate acceleration.
Now I'd like to be able to move the camera by moving my mouse to the edge of the screen. Is it possible to reuse the acceleration/smoothing code from GetAxis? I'd like those two methods to move the camera identically.
And I'd like not writing my own smoothing effect from scrach.
Answer by ImpOfThePerverse · May 11, 2018 at 03:24 PM
I'm pretty sure the input manager information is still inaccessible, as discussed in this thread:
https://forum.unity.com/threads/input-manager.166712/
What you'll need to do is create your own acceleration value and tune it to match the key acceleration.
public float timeToFullSpeed;
float speedTimer;
// Calculate mouseIsRight and mouseIsLeft from mouse screen position
void MouseMovement(bool mouseIsRIght, bool mouseIsLeft)
{
if (mouseIsRight)
{
speedTimer = Mathf.Min(timeToFullSpeed, speedTimer + Time.deltaTime);
}
else if (mouseIsLeft)
{
speedTimer = Mathf.Max(-timeToFullSpeed, speedTimer - Time.deltaTime);
}
else
{
if (speedTimer > 0)
{
speedTimer = Mathf.Max(0f, speedTimer - Time.deltaTime)
}
else
{
speedTimer = Mathf.Min(0f, speedTimer + Time.deltaTime)
}
}
transform.Translate(Vector3.Right * (speedTimer / timeToFullSpeed) * Time.deltaTime);
}
Answer by Happeloy · May 11, 2018 at 02:46 PM
Sure, no problem.
float value = Camera.main.ScreenToViewportPoint(Input.mousePosition).x;
will give you a value from 0-1, where 0 is the left side of the screen, 0.5 is the middle, and 1 is the right side of the screen.
No no, I'm trying to make a camera like in RTS games for example. You can pan with keyboard or by moving your mouse to the edge of the screen. The question is, it possible to reuse the acceleration from keyboard input, so I'm able to pan smoothly using the edges of the screen.
Well, in that case, I think that either I've misunderstood your question, or you have misunderstood what Input.GetAxis("Horizontal") does, it does not give you an acceleration, it gives you the current amount of horizontal movement on an axis. If you were to attach a gamepad to your system, and put it half way to the right, it would read 0.5.
If you want to get the acceleration of an input, you should look at the delta value of the input. In other words, you get the input position from one frame,and compare it to the input of the last frame. Something like this:
float currentvalue = Camera.main.ScreenToViewportPoint(Input.mousePosition).x;
float acceleration = lastvalue - currentvalue;
lastvalue = currentvalue;
where lastvalue is decleared globally.
I think what he's going for is having the speed ramp up over time. Using GetAxis for a keypress returns a value that ramps up from 0 to 1 at a rate deter$$anonymous$$ed by the axis' sensitivity setting, so the window movement would accelerate from zero ins$$anonymous$$d of being a simple go/no go. The code I wrote below should make the mouse-position-driven movement take timeToFullSpeed seconds to go from 0 to full speed. Just set timeToFullSpeed to half a second or so.
Your answer
Follow this Question
Related Questions
Possible To Add Move Functions To Camera? 2 Answers
Mini cam/duplicate of main camera view 0 Answers
How to get the 2d Poisition (in camera) of the mesh vertices 3 Answers
Camera wrapping 0 Answers
Unpaused camera twitch 0 Answers