SHARE
TWEET

Untitled

a guest Jun 19th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController2D : MonoBehaviour
  6. {
  7.     private Rigidbody2D rb2D;
  8.  
  9.     private Vector2 velocity;
  10.  
  11.     Vector2 xvelocity = new Vector2(10, 0);
  12.     Vector2 yvelocity = new Vector2(0, 10);
  13.  
  14.     public float jumptime;
  15.     bool grounded = true;
  16.  
  17.     bool jump = false;
  18.  
  19.     void Start()
  20.     {
  21.         rb2D = GetComponent<Rigidbody2D>();
  22.     }
  23.  
  24.     void Update()
  25.     {
  26.         if (Input.GetButton("Jump"))
  27.         {
  28.             jump = true;
  29.         }
  30.  
  31.     }
  32.  
  33.     private void FixedUpdate()
  34.     {
  35.         //Debug.Log(Input.GetAxisRaw("Vertical"));
  36.         if (left == true)
  37.         {
  38.             //Debug.Log("Left");
  39.             rb2D.MovePosition(rb2D.position + -xvelocity * Time.fixedDeltaTime);
  40.         }
  41.  
  42.         if (right == true)
  43.         {
  44.             //Debug.Log("Right");
  45.             rb2D.MovePosition(rb2D.position + xvelocity * Time.fixedDeltaTime);
  46.         }
  47.  
  48.         //if (jump == true && grounded == true)
  49.         //{
  50.         //    jumptime -= Time.fixedDeltaTime;
  51.         //    if (jumptime > 0)
  52.         //    {
  53.         //        rb2D.MovePosition(rb2D.position + yvelocity * Time.fixedDeltaTime);
  54.         //    }
  55.         //    if (jumptime <= 0)
  56.         //    {
  57.         //        jump = false;
  58.         //        jumptime = 2;
  59.         //    }
  60.  
  61.         if (jump == true && grounded == true && jumptime > 0)
  62.         {
  63.             jumptime -= Time.fixedDeltaTime;
  64.             rb2D.MovePosition(rb2D.position + yvelocity * Time.fixedDeltaTime);
  65.         } else if (jumptime <= 0)
  66.         {
  67.             jump = false;
  68.             jumptime = 2f;
  69.         }
  70.  
  71.         //if (Time.fixedDeltaTime >= 2)
  72.         //{
  73.         //    jump = false;
  74.         //    rb2D.MovePosition(rb2D.position + -yvelocity * Time.fixedDeltaTime);
  75.         //}
  76.     }
  77.  
  78.     void OnCollisionExit2D(Collision2D other)
  79.     {
  80.         Debug.Log("No longer in contact with " + other.transform.name);
  81.         jump = true;
  82.         grounded = false;
  83.     }
  84.  
  85.     void OnCollisionEnter2D(Collision2D collision)
  86.     {
  87.         if (collision.gameObject.tag == "Terrain")
  88.         {
  89.             Debug.Log("Landed");
  90.             jump = false;
  91.             grounded = true;
  92.         }
  93.     }
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top