Big head player keeps falling over. How do I fix it?

I am brand new and trying to make a game for class. The idea is the player will move left and right and objects will come at him (towards the screen) while it dodges.

Right now I have a sphere attached to a capsule as my player. The sphere and capsule are children of a “player” object located in the head, and the move script is in that object. When it moves left and right it doesn’t stay 100% vertical and if I hit the walls it tips over. I don’t want that. I want it to stay standing straight up the whole time.

Please help, My code is below.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMove : MonoBehaviour
{

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
this.transform.Translate(Vector3.left * Time.deltaTime * 10);
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
this.transform.Translate(Vector3.right * Time.deltaTime * 10);
}
}
}


If it’s a rigidbody then you can freeze the X,Y and Z rotation in the rigidbody properties. Although I think it’ll be cute to see it wobble around a little but obviously you don’t want it to keep falling over. So make it keep itself upright by constantly adjusting its angularVelocity. Something like this:

using UnityEngine;

public class StayUpright : MonoBehaviour
{
    Rigidbody rb;
    public float strength=30;

    void Start()
    {
        rb=GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        var rot = Quaternion.FromToRotation(transform.up, Vector3.up);
        rb.AddTorque((new Vector3(rot.x, 0, rot.z)-rb.angularVelocity*0.02f)*strength);
    }
}

BTW - if your character is a rigidbody then you shouldn’t use Translate to move it around. You need to AddForce instead.