Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerMovement : MonoBehaviour {
- public float movePower = 1f;
- public float jumpPower = 1f;
- Rigidbody2D rigid;
- Vector3 movement;
- bool isJuming = false;
- // Use this for initialization
- void Start ()
- {
- rigid = gameObject.GetComponent<Rigidbody2D>();
- }
- // Update is called once per frame
- void Update ()
- {
- if (Input.GetButtonDown("Jump"))
- {
- isJuming = true;
- }
- }
- void FixedUpdate()
- {
- Move();
- Jump();
- }
- void Move()
- {
- Vector3 moveVelocity = Vector3.zero;
- if (Input.GetAxisRaw("Horizontal") < 0)
- {
- moveVelocity = Vector3.left;
- }
- else if (Input.GetAxisRaw("Horizontal") > 0)
- {
- moveVelocity = Vector3.right;
- }
- transform.position += moveVelocity * movePower * Time.deltaTime;
- }
- void Jump()
- {
- if (!isJuming)
- return;
- rigid.velocity = Vector2.zero;
- Vector2 jumpVelocity = new Vector2(0, jumpPower);
- rigid.AddForce(jumpVelocity, ForceMode2D.Impulse);
- isJuming = false;
- }
- }
Add Comment
Please, Sign In to add comment