Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class controller : MonoBehaviour
- {
- public Rigidbody2D rb2d;
- public float playerSpeed;
- public float jumpPower;
- public int directionInput;
- public bool groundCheck;
- public bool facingRight = true;
- // Start is called before the first frame update
- void Start()
- {
- rb2d = GetComponent<Rigidbody2D>();
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetKey(KeyCode.Space))
- Jump();
- float moveX = Input.GetAxis("Horizontal");
- if (Input.GetKey(KeyCode.D))
- rb2d.MovePosition(rb2d.position+Vector2.right*moveX*playerSpeed*Time.deltaTime);
- if (Input.GetKey(KeyCode.A))
- rb2d.MovePosition(rb2d.position + Vector2.right * moveX * playerSpeed * Time.deltaTime);
- if ((moveX < 0) && (facingRight))
- {
- Flip();
- }
- if ((moveX > 0) && (!facingRight))
- {
- Flip();
- }
- }
- void OnCollisionEnter2D(Collision2D col)
- {
- if (col.gameObject.tag == "gnd")
- {
- groundCheck = true;
- }
- }
- void FixedUpdate()
- {
- rb2d.velocity = new Vector2(playerSpeed * directionInput, rb2d.velocity.y);
- }
- public void Move(int InputAxis)
- {
- directionInput = InputAxis;
- }
- public void Jump()
- {
- if (groundCheck)
- {
- rb2d.velocity = new Vector2(rb2d.velocity.x, jumpPower);
- }
- groundCheck = false;
- }
- void Flip()
- {
- facingRight = !facingRight;
- Vector3 theScale = transform.localScale;
- theScale.x *= -1;
- transform.localScale = theScale;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement