C
C#2y ago
Odin

❔ GetAxis wont work

public class Driver : MonoBehaviour { // Start is called before the first frame update const float MoveUnitsPerSecond = 5; float horizontalInput = Input.GetAxis("Horizontal"); float verticalInput = Input.GetAxis("Vertical");
// Update is called once per frame void Update() { //move game object as needed Vector3 position = transform.position; if (horizontalInput != 0) {
position.x += horizontalInput * MoveUnitsPerSecond * Time.deltaTime;
} if (verticalInput != 0) {
position.x += verticalInput * MoveUnitsPerSecond * Time.deltaTime; } transform.position = position;
} } Why wont GetAxis work, the course i am going off of has this exact code but unity is saying that I cannot call GetAxis("Horizontal") from a monobehavior constructor
21 Replies
Thinker
Thinker2y ago
float horizontalInput = Input.GetAxis("Horizontal");
This tries to set horizontalInput to the axis when the object is first being initialized. It doesn't really make sense to do this when it's being created, as you probably want to check for input on every frame or something.
Odin
OdinOP2y ago
how would i do that? im new to learning C#
Thinker
Thinker2y ago
horizontalInput = Input.GetAxis("Horizontal"); in Update.
Kouhai
Kouhai2y ago
You'd move the calls to Update() Though I think in unity 2023 there is a new input system and the whole GetAxis is avoided, but don't quote me on that 😅
Odin
OdinOP2y ago
It works now, thank you
Thinker
Thinker2y ago
Also, you can check out $helloworld if you want an introduction to C# as a language, independent of Unity
Odin
OdinOP2y ago
All of my focus on C# will be in Unity, i am taking a course on coursera for it Well now another problem has uprisen My sprite in unity will not move vertically
Kouhai
Kouhai2y ago
As Thinker' has explained, you need to move the code for vertical input to update as well
Odin
OdinOP2y ago
I have
Kouhai
Kouhai2y ago
Where? Ah
Odin
OdinOP2y ago
void Update() { float verticalInput = Input.GetAxis("Vertical"); float horizontalInput = Input.GetAxis("Horizontal"); //move game object as needed Vector3 position = transform.position; if (horizontalInput != 0) {
position.x += horizontalInput * MoveUnitsPerSecond * Time.deltaTime;
} if (verticalInput != 0) {
position.x += verticalInput * MoveUnitsPerSecond * Time.deltaTime; } transform.position = position;
}
Kouhai
Kouhai2y ago
You have a bug in your code you're setting position.x instead of y
Odin
OdinOP2y ago
oh i saw itt as you were typing thanks all
Thinker
Thinker2y ago
Also you don't actually need the ifs here
Kouhai
Kouhai2y ago
Yeah
Thinker
Thinker2y ago
If either of them is 0 then the entire expression will be 0 so += won't do anything
Odin
OdinOP2y ago
is that why, after changing the postion.x to y it still wont move vertically nvm i caught my error
Accord
Accord2y 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.
Odin
OdinOP2y ago
/close
Accord
Accord2y 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