Hello
I just started learning Unity, and I was trying to create an object that moves. I added the following script to the object, but when I hit play nothing happens:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementScript : MonoBehaviour
{
public float speed = 5f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector2 pos = transform.position;
pos.x += h * speed * Time.deltaTime;
pos.y += v * speed * Time.deltaTime;
transform.position = pos;
}
}
I’m using Unity 2020.3.13f1 for Mac.
Would truly appreciate any help.
Thanks a lot.
I checked exactly same code with yours and it works
Isn’t the value (h or v) * speed * Time.deltaTime is too small to your game object looks moving?
Thanks for the response chitPPO. I’m following a YouTube tutorial and it seems to be working for them. The object is visibly moving around. I tried to increase the speed however, but it still didn’t work.
First thing to do is add a Debug.Log statement to your Update method and see if it shows up in console. Don’t be afraid to add several and print out values such as position or the value of v or h.
This would be the first step to finding out if your code is even running.
What is often happening in these cases is one of the following:
- the code you think is executing is not actually executing at all
- the code is executing far EARLIER or LATER than you think
- the code is executing far LESS OFTEN than you think
- the code is executing far MORE OFTEN than you think
- the code is executing on another GameObject than you think it is
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
- is this code even running? which parts are running? how often does it run? what order does it run in?
- what are the values of the variables involved? Are they initialized? Are the values reasonable?
- are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
https://discussions.unity.com/t/839300/3
Thanks a lot for the great feedback!
Ok so the script is actually running, but the problem is that my h and v variables have a value of 0 all the time.
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
This is why the character is not moving. Any ideas why this is happening?
Thanks again.
So when you hit Left/Right keys, the values do not change? Check your Key Bindings in Player Settings, i believe.
1 Like
I’m sorry, the tutorial I’m following mentioned nothing about having to input anything, and their character seemed to be moving in a perfectly synchronous manner, so I thought the script was supposed to put the character into motion without me hitting anything. I looked up the input.getaxis command now and realized that I had no problem all along.
Sorry to have wasted your time. Thanks for the help everyone!