When I played my game, the player moved straight up, with me being unable to keep control. Would it be fixed if I removed the code for vertical? And if so how would I change the code so no other problems showed? I also do not know much code or c # so, I may have made a mistake easily fixed.
My game is 2D and all I really care about at the moment is making it move left to right.
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement1 : MonoBehaviour
{
private float speed;
public Rigidbody2D rigidbody;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector2 direction = new Vector2(horizontalInput, verticalInput);
rigidbody.velocity = direction * speed;
rigidbody.gravityScale = 0;
}
}
New contributor
Adeline Haun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1