Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class Control : Unit {
  7.  
  8. [SerializeField]
  9. private float speed = 3.0f;
  10. [SerializeField]
  11. private int lives = 5;
  12. [SerializeField]
  13. private float jumpForce = 15.0f;
  14. private bool isGrounded = false;
  15.  
  16. new private Rigidbody2D rigidbody;
  17. private Animator animator;
  18. private SpriteRenderer sprite;
  19. private int score;
  20. public Text scoreText;
  21.  
  22.  
  23. private CharState State
  24. {
  25. get { return (CharState)animator.GetInteger("State"); }
  26. set { animator.SetInteger("State", (int)value); }
  27.  
  28. }
  29. private void Awake()
  30. {
  31. rigidbody = GetComponent<Rigidbody2D>();
  32. animator = GetComponent<Animator>();
  33. sprite = GetComponentInChildren<SpriteRenderer>();
  34. score = 0;
  35. scoreText.text = "Score: " + score.ToString();
  36. }
  37.  
  38. private void FixedUpdate()
  39. {
  40. CheckGround();
  41. }
  42.  
  43. private void Update()
  44. {
  45. if (isGrounded) State = CharState.Idle;
  46.  
  47. if (Input.GetButton("Horizontal")) Run();
  48. if (isGrounded && Input.GetButton("Jump")) Jump();
  49. }
  50.  
  51. private void Run()
  52. {
  53.  
  54. Vector3 direction = transform.right * Input.GetAxis("Horizontal");
  55.  
  56. transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, speed * Time.deltaTime);
  57.  
  58. sprite.flipX = direction.x < 0.0F;
  59.  
  60. if (isGrounded) State = CharState.Run;
  61.  
  62. }
  63.  
  64. private void Jump()
  65. {
  66. rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
  67. }
  68.  
  69. private void CheckGround()
  70. {
  71. Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 0.5F);
  72.  
  73. isGrounded = colliders.Length > 1;
  74.  
  75. if (!isGrounded) State = CharState.Jump;
  76. }
  77.  
  78. void OnTriggerEnter2D(Collider2D other)
  79. {
  80. if (other.gameObject.CompareTag("Pick Up"))
  81. {
  82. Destroy(other.gameObject);
  83. score++;
  84. scoreText.text = "Score: " + score.ToString();
  85. }
  86. }
  87.  
  88. }
  89.  
  90. public enum CharState
  91. {
  92. Idle,
  93. Run,
  94. Jump,
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement