C
C#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
T
Thinker394d 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.
O
Odin394d ago
how would i do that? im new to learning C#
T
Thinker394d ago
horizontalInput = Input.GetAxis("Horizontal"); in Update.
K
Kouhai /人◕ ‿‿ ◕人\394d 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 😅
O
Odin394d ago
It works now, thank you
T
Thinker394d ago
Also, you can check out $helloworld if you want an introduction to C# as a language, independent of Unity
O
Odin394d 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
K
Kouhai /人◕ ‿‿ ◕人\394d ago
As Thinker' has explained, you need to move the code for vertical input to update as well
O
Odin394d ago
I have
K
Kouhai /人◕ ‿‿ ◕人\394d ago
Where? Ah
O
Odin394d 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;
}
K
Kouhai /人◕ ‿‿ ◕人\394d ago
You have a bug in your code you're setting position.x instead of y
O
Odin394d ago
oh i saw itt as you were typing thanks all
T
Thinker394d ago
Also you don't actually need the ifs here
K
Kouhai /人◕ ‿‿ ◕人\394d ago
Yeah
T
Thinker394d ago
If either of them is 0 then the entire expression will be 0 so += won't do anything
O
Odin394d ago
is that why, after changing the postion.x to y it still wont move vertically nvm i caught my error
A
Accord393d 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.
O
Odin388d ago
/close
A
Accord387d 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
More Posts
✅ (SOLVED)Help a Beginneri am following CodeMonkeys 11Hr youtube video on coding a game in unity. I have ran into a issue whe❔ Exclude items from linq query if they contain one from an array of substringsI've got a list of strings, that if they appear anywhere in a linq query column i want to exclude th✅ Comprehension questions (LeetCode Binary Search)Like the title says it's about this puzzle https://leetcode.com/problems/binary-search/description/ ❔ How to create a cubic function calculator that also calculates the minimum and maximum value```c Console.WriteLine("Please enter a, b, c, d for the cubic function ax^3+bx^2+cx+d"); ❔ how to listen to the output of a virtual audio device on windows using C#?how to listen to the output of a virtual audio device on windows using C# to save the raw audio stre❔ How to wait for process to completei want to make program opens async but instead of giving 'hard coded' delays but i don't know how to❔ Problem with including C DLL in a C# console app project.So I have this C DLL imported into my console app (.NET 5). I simply cannot get to call it correctly✅ Win Form Event Handlers questionHi, I was just wondering if there is an event handler when the win form is idle that I can then run ❔ Problem with Dictionary<ulong, Image> (Strange work of memory access)Concept of my system: A system that draws a picture with stickers, each of the stickers can be moved❔ How do I refactor this?I am working with a grid of tiles, each tile has an int, which is made up of 4 bytes which represent❔ [AvaloniaUI] Visibility of item in ListView based on conditionAn Avalonia UI application is used to manage an evidence of items. It allows the user to add items. ✅ How to structure this login/signup page layoutSo I have this mockup of the layout I want for a login/signup page in my Blazor WASM app. Pretty sta❔ I am need of help before I give up and run my code on a server instead of a serverless solution.I have create an azure function locally and i've used the selenuim webdriver package for taking scre✅ .Net Core 6 Asymmetric Encryption with custom public and private keysHello all! How can i use the `Asymmetric Encryption` in .Net 6 but choosing/importing private and pu❔ No Design in C#Instances of this bug (1) 1. View Call Stack at System.Runtime.InteropServices.Marshal.ThrowExce❔ having issues on a reloading script on unity to reload weaponsthe code is used in a youtube video and i have pretty much copied to to get it to work but it doesnt❔ help with an exerciseI've encountered a weird problem that idk how to fix. Say we've got a string "31131123521" how do i ❔ Accessing HTTP Context at DbCommandInterceptor [.NET 7]Hi! I'm having some issues trying to access the HTTP Context at my DbCommand Interceptor class. What❔ XMLAttributeCollection -> Dictionary with LINQ?It's a confusing class. It only allows turning it into a Queryable, but I have no experience with th❔ MS SQL Reporting Server URL Being RedirectedWe have an application from a vendor that was written in C# and we recently upgraded the MS SQL data