daily pastebin goal
6%
SHARE
TWEET

Untitled

a guest Jan 23rd, 2019 72 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. using UnityEngine.UI;
  5.  
  6. public class collector : MonoBehaviour {
  7.  
  8.     public float speed;
  9.     public float jumpspeed;
  10.     private Vector2 input = new Vector2(0,0);
  11.     private Rigidbody2D rb;
  12.     private bool grounded;
  13.     public int score;
  14.     public Text scoreText;
  15.     // Use this for initialization
  16.     void Start () {
  17.         rb = gameObject.GetComponent<Rigidbody2D>();
  18.     }
  19.    
  20.     // Update is called once per frame
  21.     void Update () {
  22.         input = rb.velocity;
  23.  
  24.         if(Input.GetKey(KeyCode.D)){
  25.             input.x = speed;
  26.         }
  27.         else if (Input.GetKey(KeyCode.A)){
  28.             input.x = -speed;
  29.         }
  30.         else if (Input.GetKey(KeyCode.W)& grounded == true) {
  31.             input.y = jumpspeed;
  32.         }
  33.         rb.velocity = input;
  34.     }
  35.  
  36.     void OnCollisionEnter2D(Collision2D collision){
  37.         grounded = true;
  38.     }
  39.  
  40.     void OnCollisionExit2D(Collision2D collision){
  41.         grounded = false;
  42.     }
  43.  
  44.     void OnTriggerEnter2D(Collider2D collision){
  45.         if(collision.gameObject.name == "carrot"){
  46.             score++;
  47.             scoreText.text = score.ToString();
  48.             Destroy(collision.gameObject); 
  49.         }
  50.     }
  51. }
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