Operator * cannot be applied to operands of type float and bool
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)
}
Answer by andzq · Oct 15, 2016 at 04:25 PM
Input.GetKeyDown(KeyCode.LeftShift)
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);
}
}
Thanks, though the controls were swapped (Z had to by in Y's place and vice-versa) it works.
Your answer
Follow this Question
Related Questions
Shader properties - float or vector? 0 Answers
-Then- statement for scipts. 1 Answer
Vector2.Distance syntax {java} 2 Answers