C
C#8mo ago
Thundr

❔ im extremely new to c#/unity, i can't find the issue with this code

if (Keyboard.IsKeyDown(Key.S))
{
transform.Translate(Vector.3.back * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);
}
if (Keyboard.IsKeyDown(Key.S))
{
transform.Translate(Vector.3.back * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);
}
the transform.Translate line has the syntax error, ',' expected error, and i cant find the issue
40 Replies
SinFluxx
SinFluxx8mo ago
Vector.3.back Should that just be Vector3.back ?
Thundr
Thundr8mo ago
ohh my bad thank you yep im just dumb sorry im rly new to this 💀
SinFluxx
SinFluxx8mo ago
blobthumbsup
Thundr
Thundr8mo ago
yo like while im on the topic the whole script has no errors but it dont work
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 3f;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
transform.Translate(Vector3.left * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);

}

if (Input.GetKeyDown(KeyCode.D))
{
transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);
}

if (Input.GetKeyDown(KeyCode.W))
{
transform.Translate(Vector3.forward * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);
}

if (Input.GetKeyDown(KeyCode.S))
{
transform.Translate(Vector3.back * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 3f;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
transform.Translate(Vector3.left * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);

}

if (Input.GetKeyDown(KeyCode.D))
{
transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);
}

if (Input.GetKeyDown(KeyCode.W))
{
transform.Translate(Vector3.forward * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);
}

if (Input.GetKeyDown(KeyCode.S))
{
transform.Translate(Vector3.back * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);
}
}
}
TheRanger
TheRanger8mo ago
did you attach the script to the player?
cap5lut
cap5lut8mo ago
the script itself should indeed work if its attached to the game object (tho for W and S u want the vertical axis i would assume)
TheRanger
TheRanger8mo ago
the code doesnt make sense for example Input.GetAxis("Horizontal") will always return 0 unless you hold down the key that is mapped to it
TheRanger
TheRanger8mo ago
No description
TheRanger
TheRanger8mo ago
the question is, did you map the horizontal axis to D and A like in this image?
Thundr
Thundr8mo ago
ill check that one sec its attached, doesnt work how tf do i even get here 😭
Thundr
Thundr8mo ago
yes ok so what should i do about that
TheRanger
TheRanger8mo ago
can you show a screenshot to that
Thundr
Thundr8mo ago
No description
TheRanger
TheRanger8mo ago
ensure Update method is being called use Debug.Log or something
Thundr
Thundr8mo ago
alr 1s where do i see the debug outcome
TheRanger
TheRanger8mo ago
in where the error messages window
TheRanger
TheRanger8mo ago
No description
Thundr
Thundr8mo ago
i didnt get any errors running it ok i see it yeah the updates are being called
TheRanger
TheRanger8mo ago
now try to debug if the getkeydown is being called and debug the values that are placed in the Translate method
Thundr
Thundr8mo ago
ok A and D work W doesnt neither does S
TheRanger
TheRanger8mo ago
define doesnt work
Thundr
Thundr8mo ago
uh debug message not showing up
TheRanger
TheRanger8mo ago
where did you place the debug.log ?
Thundr
Thundr8mo ago
if (Input.GetKeyDown(KeyCode.S))
{
Debug.Log("Hello: " + gameObject.name);
transform.Translate(Vector3.back * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);
}
if (Input.GetKeyDown(KeyCode.S))
{
Debug.Log("Hello: " + gameObject.name);
transform.Translate(Vector3.back * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);
}
TheRanger
TheRanger8mo ago
so when you press/hold S, it doesnt print ?
Thundr
Thundr8mo ago
yep
TheRanger
TheRanger8mo ago
weird
Thundr
Thundr8mo ago
same with W but not A and D
TheRanger
TheRanger8mo ago
bigthonk
Thundr
Thundr8mo ago
thast what im saying
TheRanger
TheRanger8mo ago
btw for S and W it should be Input.GetAxis("Vertical") not Input.GetAxis("Horizontal")
Thundr
Thundr8mo ago
okay rly casue im tryna do a 3d WASD movement thing on the ground not flying
TheRanger
TheRanger8mo ago
did you save the file?
Thundr
Thundr8mo ago
yeah
TheRanger
TheRanger8mo ago
usually u save with ctrl + s
Thundr
Thundr8mo ago
i go to file and press save then go back to unity
TheRanger
TheRanger8mo ago
very weird
Thundr
Thundr8mo ago
yeah fr
Accord
Accord8mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.