Why make it so complicated? Why dont you use Input.GetAxis and save you the trouble? I will make it for you since it is properly the first time you are using it.
Oh and dont use transform.position to alter movements(it is generally not a good idea). transform.Translate is way better and easier in my opinion.
var rotationSpeed = 10.0;
var moveSpeed = 7.0;
function Update (){
var forSpeed = Input.GetAxis("Vertical")
transform.Translate(transform.forward * forSpeed * moveSpeed * Time.deltaTime);
var turnSpeed = Input.GetAxis("Horizontal");
transform.eulerAngles.y += turnSpeed * rotationSpeed * Time.deltaTime;
}
"Oh and dont use transform.position to alter movements(it is generally not a good idea)" Each has its uses. Setting the position directly is correct when you want to do your own interpolation per frame, for example. Never use it on a rigidbody though.
then the problem lies somewhere else and not in this code snippet. I bet that the input is assigned incorrectly. are you using the new or old input system?
Additionally, I believe the error is because you have parenthesis after deltaTime, remove those and it should clear that particular error.
Also, your code has euglerAngles, not eulerAngles.
Besides that, I agree with the previous answer, using Translate() and Rotate() is probably better than manipulating the position and rotation directly.
Also, I found the following script from Graham McAllister’s tutorial helpful; I think it’s the same concept as above:
var speed = 5.0;
function Update () {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(x, 0, z);
}
I have a follow up question; how would I make the script operate with mouse movements instead of keyboard keys? Right now, by default, the input axes are controlled by w,a,s,d keys. To make it so that the user can use the mouse to move, do I just change the input settings in the input manager, or do I change the code to include an “OnMouse…” function? But, which function is it that just allows the user to not click a mouse button, but just move it around?
"Oh and dont use transform.position to alter movements(it is generally not a good idea)" Each has its uses. Setting the position directly is correct when you want to do your own interpolation per frame, for example. Never use it on a rigidbody though.
– RafesFor characters "it is generally not a good idea". If used in FPS' the players will phase through objects.
– FLASHDENMARKthen the problem lies somewhere else and not in this code snippet. I bet that the input is assigned incorrectly. are you using the new or old input system?
– anszwaTest with Debug.Log whether the horizontal and vertical input also works in the negative direction.
– anszwa