C
C#13mo 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
SinFluxx13mo ago
Vector.3.back Should that just be Vector3.back ?
Thundr
ThundrOP13mo ago
ohh my bad thank you yep im just dumb sorry im rly new to this 💀
SinFluxx
SinFluxx13mo ago
blobthumbsup
Thundr
ThundrOP13mo 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
TheRanger13mo ago
did you attach the script to the player?
cap5lut
cap5lut13mo 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
TheRanger13mo 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
TheRanger13mo ago
No description
TheRanger
TheRanger13mo ago
the question is, did you map the horizontal axis to D and A like in this image?
Thundr
ThundrOP13mo ago
ill check that one sec its attached, doesnt work how tf do i even get here 😭
Thundr
ThundrOP13mo ago
yes ok so what should i do about that
TheRanger
TheRanger13mo ago
can you show a screenshot to that
Thundr
ThundrOP13mo ago
No description
TheRanger
TheRanger13mo ago
ensure Update method is being called use Debug.Log or something
Thundr
ThundrOP13mo ago
alr 1s where do i see the debug outcome
TheRanger
TheRanger13mo ago
in where the error messages window
TheRanger
TheRanger13mo ago
No description
Thundr
ThundrOP13mo ago
i didnt get any errors running it ok i see it yeah the updates are being called
TheRanger
TheRanger13mo ago
now try to debug if the getkeydown is being called and debug the values that are placed in the Translate method
Thundr
ThundrOP13mo ago
ok A and D work W doesnt neither does S
TheRanger
TheRanger13mo ago
define doesnt work
Thundr
ThundrOP13mo ago
uh debug message not showing up
TheRanger
TheRanger13mo ago
where did you place the debug.log ?
Thundr
ThundrOP13mo 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
TheRanger13mo ago
so when you press/hold S, it doesnt print ?
Thundr
ThundrOP13mo ago
yep
TheRanger
TheRanger13mo ago
weird
Thundr
ThundrOP13mo ago
same with W but not A and D
TheRanger
TheRanger13mo ago
bigthonk
Thundr
ThundrOP13mo ago
thast what im saying
TheRanger
TheRanger13mo ago
btw for S and W it should be Input.GetAxis("Vertical") not Input.GetAxis("Horizontal")
Thundr
ThundrOP13mo ago
okay rly casue im tryna do a 3d WASD movement thing on the ground not flying
TheRanger
TheRanger13mo ago
did you save the file?
Thundr
ThundrOP13mo ago
yeah
TheRanger
TheRanger13mo ago
usually u save with ctrl + s
Thundr
ThundrOP13mo ago
i go to file and press save then go back to unity
TheRanger
TheRanger13mo ago
very weird
Thundr
ThundrOP13mo ago
yeah fr
Accord
Accord13mo 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.
Want results from more Discord servers?
Add your server