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 PlayerController2D : MonoBehaviour
- {
- private Rigidbody2D rb2D;
- private Vector2 velocity;
- Vector2 xvelocity = new Vector2(10, 0);
- Vector2 yvelocity = new Vector2(0, 10);
- public float jumptime;
- bool grounded = true;
- bool jump = false;
- void Start()
- {
- rb2D = GetComponent<Rigidbody2D>();
- }
- void Update()
- {
- if (Input.GetButton("Jump"))
- {
- jump = true;
- }
- }
- private void FixedUpdate()
- {
- //Debug.Log(Input.GetAxisRaw("Vertical"));
- if (left == true)
- {
- //Debug.Log("Left");
- rb2D.MovePosition(rb2D.position + -xvelocity * Time.fixedDeltaTime);
- }
- if (right == true)
- {
- //Debug.Log("Right");
- rb2D.MovePosition(rb2D.position + xvelocity * Time.fixedDeltaTime);
- }
- //if (jump == true && grounded == true)
- //{
- // jumptime -= Time.fixedDeltaTime;
- // if (jumptime > 0)
- // {
- // rb2D.MovePosition(rb2D.position + yvelocity * Time.fixedDeltaTime);
- // }
- // if (jumptime <= 0)
- // {
- // jump = false;
- // jumptime = 2;
- // }
- if (jump == true && grounded == true && jumptime > 0)
- {
- jumptime -= Time.fixedDeltaTime;
- rb2D.MovePosition(rb2D.position + yvelocity * Time.fixedDeltaTime);
- } else if (jumptime <= 0)
- {
- jump = false;
- jumptime = 2f;
- }
- //if (Time.fixedDeltaTime >= 2)
- //{
- // jump = false;
- // rb2D.MovePosition(rb2D.position + -yvelocity * Time.fixedDeltaTime);
- //}
- }
- void OnCollisionExit2D(Collision2D other)
- {
- Debug.Log("No longer in contact with " + other.transform.name);
- jump = true;
- grounded = false;
- }
- void OnCollisionEnter2D(Collision2D collision)
- {
- if (collision.gameObject.tag == "Terrain")
- {
- Debug.Log("Landed");
- jump = false;
- grounded = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement