Question by
Zequintex · Jun 05, 2017 at 10:17 AM ·
unity5movement script
Speed Increasement
I made it so I move faster when holding shift and set a maximum number that it should rise to which was 5 but it doesn't seem to work, any ideas? (I'm new to this C#)
if ((Input.GetKey(KeyCode.LeftShift)))
{
movementSpeed += .01f;
if (movementSpeed == 5f)
{
movementSpeed = 5f;
}
Comment
Best Answer
Answer by Vojtasle · Jun 05, 2017 at 11:23 AM
The 1st thing I see is I would totaly not use if (movementSpeed == 5f), you can never be sure that you will get exact 5f. You have to use if(movementSpeed >= 5f).
Then it depends on how you use movementSpeed for moving with gameObject but I would recommend using Add force or Forward . If you would post more code, I would be more helpful :). Following code is example of how it should be working if you add moving with object.
private int movementSpeed = 0;//set speed you want to set, if you set it as public, you will be able to change the value from unity too
void Update()
{
if ((Input.GetKey(KeyCode.LeftShift)))
{
movementSpeed += 0.01f;
if (movementSpeed >= 5f)
{
movementSpeed = 5f;
}
}
if (Input.GetKey(KeyCode.A))
{
//move left
Debug.Log("Pressing A");
}
if (Input.GetKey(KeyCode.W))
{
//move straight
Debug.Log("Pressing W");
}
if (Input.GetKey(KeyCode.S))
{
//move back
Debug.Log("Pressing S");
}
if (Input.GetKey(KeyCode.D))
{
//move right
Debug.Log("Pressing D");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612123535im_/https://answers.unity.com/themes/thub/images/avi.jpg)