Check out example codes for "unity movement script 2d". It will help you in understanding the concepts better.
Code Example 1
public float MoveSpeed;
public float jumpPower;
public bool Cground = false;
void Start()
{
}
void Udpate ()
{
Jump();
Vector3 Move = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
transform.postion += Move * Time.deltaTime * MoveSpeed;
}
void Jump ()
{
if (Input.GetKeyDown(KeyCode.Space) && Cground = true)
{
gameObject.AddForce(new Vector2(0f, JumpPower), ForceMode2D.Impulse);
Cground = false;
}
}
void OnCollisionEnter2D(Collision2D other)
{
Cground = true
}
Code Example 2
//player must have a rigidbody2D and a box colider
public float moveSpeed = 5f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Jump();
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position += movement * Time.deltaTime * moveSpeed;
}
void Jump()
{
if (Input.GetButtonDown("Jump"))
{
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
}
}
Learn ReactJs, React Native from akashmittal.com