How would you add gravity to stop rising when moving forward?

I’m an extreme noob, literally been using Unity for a few days and learning the scripting… I created this as I thought it’d work to move left, right forward and back, and it does… but it rises into the air when I go forward lol. What kinda code would you use to stop that? Here’s what I got (Sorry if it’s badly written)

using UnityEngine;
using System.Collections;

public class playerMove : MonoBehaviour {

	public float moveSpeed; 
	public float stopMoving; 

	// Update is called once per frame
	void Update () {

		if (Input.GetKey(KeyCode.W)){

			rigidbody.velocity = transform.forward * moveSpeed; 

		}

		else if (Input.GetKey(KeyCode.S)){
			rigidbody.velocity = transform.forward * -moveSpeed; 
		}

		else if (Input.GetKey(KeyCode.D)){
			rigidbody.velocity = transform.right * moveSpeed; 
		}

		else if (Input.GetKey(KeyCode.A)){
			rigidbody.velocity = transform.right * -moveSpeed;
		}

		else {
			rigidbody.velocity = transform.forward * stopMoving; 
		}
	}
}

You can stop this by ticking boxes in the objects Rigidbody → Constraints

Try ticking the Freeze Rotation boxes

I assume by ‘rises in the air’ you mean it twists and goes up like a plane taking off.

No xD Haha, I mean so the player would stick to the ground unless I jump, otherwise when I point my camera towards the sky he flies like E.T lol

OK maybe you should have mentioned that before.

Your code has no jump functionality in it so without knowing how you are going to handle jumping its hard to give any help.

Its can be done with a raycast to see if the object is grounded thus allowing the player to jump if it is, you can also use rays to keep the object from floating away in a similar fashion.

Is your rigidbody checked to use gravity?

Its not going to make any difference because he is setting velocity directly, which will override any gravity forces generated.

you need to add some downward velocity to keep it moving… down.

The problem is that when you look up, the transform.forward also looks up, so increasing the velocity in that direction will make you fly. Without knowing any more specifics, I would just advise you to use the character controller Unity has built-in, in the assets page.