using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class player_good : MonoBehaviour { public float speed ; public float JumpForce; Rigidbody2D rb; bool OnGround; private Animator anim; SpriteRenderer sprite; [SerializeField] private Text counter; [SerializeField]private AudioSource jumpSoundeffect; [SerializeField]private AudioSource collectSoundeffect; [SerializeField]private AudioSource endSoundeffect; private int count=0; // Start is called before the first frame update void Start() { rb=GetComponent<Rigidbody2D>(); OnGround=true; sprite = GetComponent<SpriteRenderer>(); anim=GetComponent<Animator>(); } // Update is called once per frame void Update() { float player =Input.GetAxis("Horizontal"); transform.position += new Vector3(player*speed,0,0); if (Input.GetKeyDown(KeyCode.Space)&&OnGround) { jumpSoundeffect.Play(); rb.AddForce(new Vector2(0,JumpForce)); anim.SetTrigger("jump"); } //flipping if (player>0) { sprite.flipX=false; } else if (player<0) { sprite.flipX=true; } if(player!=0) { anim.SetBool("is wolking",true); } else { anim.SetBool("is wolking",false); } } private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.tag=="Ground") { OnGround=true; } if (collision.gameObject.CompareTag("end")) { endSoundeffect.Play(); SceneManager.LoadScene("SampleScene"); } } private void OnCollisionExit2D(Collision2D collision) { if (collision.gameObject.tag=="Ground") { OnGround=false; } } private void OnTriggerEnter2D (Collider2D collision) { if(collision.gameObject.CompareTag("fruit")) { collectSoundeffect.Play(); count++; Destroy(collision.gameObject); counter.text="Fruits"+count; } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)