'Move object in Unity 2D
I have made this script to move my player with no physics involved:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour
{
[SerializeField] private float speed;
private float horinzontal;
private float vertical;
private void Update()
{
vertical = Input.GetAxis("Vertical");
horinzontal = Input.GetAxis("Horizontal");
if (horinzontal > 0.01f)
transform.localScale = Vector3.one;
else if (horinzontal < -0.01f)
transform.localScale = new Vector3(-1, 1, 1);
}
}
But my player is not moving, he is just turning left and right?
Why does it happen and how can I fix this issue?
Solution 1:[1]
Right now, your code is just changing what direction the player faces since you're editing transform.localScale (how big the object is / what direction your object faces). You want to edit transform.position of the GameObject (the location of your object).
Assuming that this is a 2D project, for basic horizontal movement you can do the following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float speed = 5f;
private void Update()
{
if (Input.GetAxis("Horizontal") > 0.01f)
{
transform.localScale = Vector3.one;
transform.position = new Vector2(transform.position.x + (speed * Time.deltaTime), transform.position.y);
}
else if (Input.GetAxis("Horizontal") < -0.01f)
{
transform.localScale = new Vector3(-1, 1, 1);
transform.position = new Vector2(transform.position.x - (speed * Time.deltaTime), transform.position.y);
}
}
}
This will be enough to get you started. If you want to be sure you understand how this works, try adding vertical movement in yourself, the code is very similar to what I've written here (even if you don't need vertical movement its good for practice).
This approach is by no means perfect, or even good. You will need to fine tune how the character moves to what you want in your game. I highly recommend you try some of the Unity tutorials to help familiarize yourself with C# and Unity.
I especially recommend the Unity microgame tutorials if longer projects seem too daunting. It will give you a crash course in the basics and you'll finish the courses with a collection of games you can modify yourself. Also note, that you can access official courses and lessons directly from Unity Hub.
Solution 2:[2]
You do not seem to have made any movement changes, only the size has changed. To understand the current problem, add the input vector to transform.position. I tried to apply this change in the summary of the code below, but there is a more principled way to use Rigidbody2D, and in this case, read the link below.
[SerializeField] private float speed = 1;
private float horinzontal;
private float vertical;
private void Update()
{
vertical = Input.GetAxis("Vertical");
horinzontal = Input.GetAxis("Horizontal");
var _sign = Mathf.Sign(horinzontal);
if (_sign == 0) return;
transform.localScale = new Vector3(_sign, 1);
transform.position += new Vector3(_sign*speed*Time.deltaTime, 0);
}
more information about Rigidbody2D: https://docs.unity3d.com/Manual/class-Rigidbody2D.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | BreakingBeaker |
Solution 2 |