❔ 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
// 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
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.how would i do that?
im new to learning C#
horizontalInput = Input.GetAxis("Horizontal");
in Update
.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 😅It works now, thank you
Also, you can check out $helloworld if you want an introduction to C# as a language, independent of Unity
Written interactive course https://learn.microsoft.com/en-us/users/dotnet/collections/yz26f8y64n7k07
Videos https://dotnet.microsoft.com/learn/videos
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
As Thinker' has explained, you need to move the code for vertical input to update as well
I have
Where?
Ah
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;
}
position.x += horizontalInput * MoveUnitsPerSecond * Time.deltaTime;
} if (verticalInput != 0) {
position.x += verticalInput * MoveUnitsPerSecond * Time.deltaTime; } transform.position = position;
}
You have a bug in your code
you're setting position.x instead of y
oh
i saw itt as you were typing
thanks all
Also you don't actually need the ifs here
Yeah
If either of them is 0 then the entire expression will be 0
so += won't do anything
is that why, after changing the postion.x to y it still wont move vertically
nvm
i caught my error
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./close
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.