- Home /
Set MINIMUM speed?
GetComponent<Rigidbody> ().AddForce (Input.GetAxis ("Mouse X")+3000, 0f, Input.GetAxis ("Mouse Y" )+3000);
Don't work properly. Player start moving even if i don't move mouse at all.
Answer by GiyomuGames · Aug 20, 2015 at 07:15 AM
You are adding 3000 to the mouse movement values so it is normal that the player moves even if the mouse doesn't move. What are you trying to do?
if i move mouse just a little, i want player to move, but not too slow because i can control mouse movement, i can easily move mouse slowly, and player will move slowly too even if i add " * 10000 ", so i want it to move with at least certain $$anonymous$$imum speed even with little movement of mouse
Ok then you need to put a bit of logic like
float $$anonymous$$Speed = 30f; // the $$anonymous$$imum speed
float speed = 20f; // the additional speed if the mouse moves by "1"
float movementThreshold = .1f; // the player will move only if the mouse moves more than this value
float movementX = Input.GetAxis ("$$anonymous$$ouse X");
if ($$anonymous$$athf.Abs(movementX) > movementThreshold)
{
movementX = $$anonymous$$athf.Sign(movementX) * ($$anonymous$$Speed + ($$anonymous$$athf.Abs(movementX) - movementThreshold) * speed);
}
float movementY = Input.GetAxis ("$$anonymous$$ouse Y");
if ($$anonymous$$athf.Abs(movementY) > movementThreshold)
{
movementY = $$anonymous$$athf.Sign(movementY) * ($$anonymous$$Speed + ($$anonymous$$athf.Abs(movementY) - movementThreshold) * speed);
}
GetComponent<Rigidbody> ().AddForce (movementX, 0f, movementY);
Sorry I can't tell if this is exactly what you need or if it will work but it should get you closer to what you are trying to do. Basically it detects when the mouse moves more than a specific value (movementThreshold). If so then the force is at least $$anonymous$$Speed and you need to also add a force depending how fast the mouse is moving above the threshold.
I will need to rework this, but it might help, not sure yet.