This should probably be an extremely easy fix, but I’m too daft to notice it…
My script is a blatant copy+paste of the Unity Player Movement tutorial script with a few minor edits and additions to allow Y axis movement from the WASD keys while the X axis would be controlled by the shift key.
Could anybody lend me a hand here?
{
var y = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
var x = Input.GetKeyDown(KeyCode.LeftShift) * Time.deltaTime * 70f;
transform.Rotate(y, 0, 0);
transform.Translate(0, 0, z);
transform.Rotate(0, x, 0)
}
will return a boolean - True or False. It therefore cannot be multiplied by your Time.deltaTime*70f.
A solution for this would be to check if LeftShift is pressed and if so, rotate by an amount that is Time.deltaTime*70f.
{
var y = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
var x = 0f;
if (Input.GetKeyDown(KeyCode.LeftShift))
x = Time.deltaTime * 70f;
transform.Rotate(y, 0, 0);
transform.Translate(0, 0, z);
transform.Rotate(0, x, 0);
}
to make tweeking of the values a little bit easier i would also suggest that you declare public variables of your multiply-values at the top of your script. This way you can customize these in the inspector so you don´t need to change your code all the time.
public class Test : MonoBehaviour
{
public float xFactor = 70f;
public float yFactor = 150f;
public float zFactor = 3f;
void Update()
{
var y = Input.GetAxis("Horizontal") * Time.deltaTime * yFactor;
var z = Input.GetAxis("Vertical") * Time.deltaTime * zFactor;
var x = 0f;
if (Input.GetKeyDown(KeyCode.LeftShift))
x = Time.deltaTime * xFactor;
transform.Rotate(y, 0, 0);
transform.Translate(0, 0, z);
transform.Rotate(0, x, 0);
}
}