Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Movement : MonoBehaviour
- {
- [SerializeField] float movement;
- [SerializeField] Rigidbody2D rigid;
- [SerializeField] int speed;
- [SerializeField] bool isFacingRight = true;
- [SerializeField] bool jumpPressed = false;
- [SerializeField] float jumpForce = 500.0f;
- [SerializeField] bool isGrounded = true;
- [SerializeField] bool shiftPressed = false;
- // Start is called before the first frame update
- void Start()
- {
- if (rigid == null)
- rigid = GetComponent<Rigidbody2D>();
- speed = 15;
- }
- // Update is called once per frame
- void Update()
- {
- movement = Input.GetAxis("Horizontal");
- if (Input.GetButtonDown("Jump"))
- jumpPressed = true;
- }
- //called potentially multiple times per frame
- //used for physics & movement
- void FixedUpdate()
- {
- rigid.velocity = new Vector2(movement * speed, rigid.velocity.y);
- if (movement < 0 && isFacingRight || movement > 0 && !isFacingRight)
- Flip();
- if (jumpPressed && isGrounded)
- Jump();
- }
- void Flip()
- {
- transform.Rotate(0, 180, 0);
- isFacingRight = !isFacingRight;
- }
- void Jump()
- {
- rigid.velocity = new Vector2(rigid.velocity.x, 0);
- rigid.AddForce(new Vector2(0, jumpForce));
- isGrounded = false;
- jumpPressed = false;
- }
- private void OnCollisionEnter2D(Collision2D collision)
- {
- if (collision.gameObject.tag == "Ground")
- {
- isGrounded = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment