Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class collector : MonoBehaviour {
- public float speed;
- public float jumpspeed;
- private Vector2 input = new Vector2(0,0);
- private Rigidbody2D rb;
- private bool grounded;
- public int score;
- public Text scoreText;
- // Use this for initialization
- void Start () {
- rb = gameObject.GetComponent<Rigidbody2D>();
- }
- // Update is called once per frame
- void Update () {
- input = rb.velocity;
- if(Input.GetKey(KeyCode.D)){
- input.x = speed;
- }
- else if (Input.GetKey(KeyCode.A)){
- input.x = -speed;
- }
- else if (Input.GetKey(KeyCode.W)& grounded == true) {
- input.y = jumpspeed;
- }
- rb.velocity = input;
- }
- void OnCollisionEnter2D(Collision2D collision){
- grounded = true;
- }
- void OnCollisionExit2D(Collision2D collision){
- grounded = false;
- }
- void OnTriggerEnter2D(Collider2D collision){
- if(collision.gameObject.name == "carrot"){
- score++;
- scoreText.text = score.ToString();
- Destroy(collision.gameObject);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement