- Home /
Programming Movement Momentum
Hi guys, I'm trying to program solely horizontal momentum in my infinite runner that takes place inside an endless cylinder. Currently the game crashes with this code, and is extremely unoptimized. The goal is to have momentum speed up logarithmicly until Movement Horizontal finally reaches normal speed after one second of holding either momentum direction down.
public class Controls : MonoBehaviour
{
public float horizVelo;
public float lefthorizVelo;
public float righthorizVelo;
public float movementHorizontal;
void Start ()
{
Mathf.Clamp (lefthorizVelo, 0, 1);
Mathf.Clamp (righthorizVelo, 0, 1);
}
void Update ()
{
while (lefthorizVelo != 1 && Input.GetKey("a"))
{
lefthorizVelo = Mathf.Log (Time.deltaTime);
}
while (righthorizVelo != 1 && Input.GetKey("d"))
{
righthorizVelo = Mathf.Log (Time.deltaTime);
}
if (Input.GetKey ("a"))
{
movementHorizontal = Input.GetAxis ("Horizontal") * actualSpeed * Time.deltaTime * lefthorizVelo;
}
else if(Input.GetKey("d"))
{
movementHorizontal = Input.GetAxis ("Horizontal") * actualSpeed * Time.deltaTime * righthorizVelo;
}
transform.RotateAround (Vector3.zero, Vector3.forward, movementHorizontal);
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613185753im_/https://answers.unity.com/themes/thub/images/avi.jpg)