hello everyone i started to learn unity game engine and c# and i follow a video course step by step but when i press the play button its immediatly center the game object and when i try to move its moving but always returning to center(position 0,0,0)
public class PlayerMovment : 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;
}
}
CodePudding user response:
You need to add your movement to the position, not overwriting it. Try this:
Vector2 pos = transform.position;
pos.x = h * speed * Time.deltaTime;
pos.y = v * speed * Time.deltaTime;
transform.position = pos;