Advertisement
Guest User

Meatboy Controller Script

a guest
Jan 2nd, 2015
4,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. /********************************************************
  2. * 2D Meatboy style controller written entirely by Nyero.
  3. *
  4. * Thank you for using this script, it makes me feel all
  5. * warm and happy inside. ;)
  6. * -Nyero
  7. *
  8. * ------------------------------------------------------
  9. * Notes on usage:
  10. * Please don't use the meatboy image, as your some
  11. * might consider it stealing. Simply replace the sprite
  12. * used, and you'll have a 2D platform controller that is
  13. * very similar to meatboy.
  14. ********************************************************/
  15. using UnityEngine;
  16. using System.Collections;
  17.  
  18. public class Controller : MonoBehaviour
  19. {
  20. public class GroundState
  21. {
  22. private GameObject player;
  23. private float width;
  24. private float height;
  25. private float length;
  26.  
  27. //GroundState constructor. Sets offsets for raycasting.
  28. public GroundState(GameObject playerRef)
  29. {
  30. player = playerRef;
  31. width = player.collider2D.bounds.extents.x + 0.1f;
  32. height = player.collider2D.bounds.extents.y + 0.2f;
  33. length = 0.05f;
  34. }
  35.  
  36. //Returns whether or not player is touching wall.
  37. public bool isWall()
  38. {
  39. bool left = Physics2D.Raycast(new Vector2(player.transform.position.x - width, player.transform.position.y), -Vector2.right, length);
  40. bool right = Physics2D.Raycast(new Vector2(player.transform.position.x + width, player.transform.position.y), Vector2.right, length);
  41.  
  42. if(left || right)
  43. return true;
  44. else
  45. return false;
  46. }
  47.  
  48. //Returns whether or not player is touching ground.
  49. public bool isGround()
  50. {
  51. bool bottom1 = Physics2D.Raycast(new Vector2(player.transform.position.x, player.transform.position.y - height), -Vector2.up, length);
  52. bool bottom2 = Physics2D.Raycast(new Vector2(player.transform.position.x + (width - 0.2f), player.transform.position.y - height), -Vector2.up, length);
  53. bool bottom3 = Physics2D.Raycast(new Vector2(player.transform.position.x - (width - 0.2f), player.transform.position.y - height), -Vector2.up, length);
  54. if(bottom1 || bottom2 || bottom3)
  55. return true;
  56. else
  57. return false;
  58. }
  59.  
  60. //Returns whether or not player is touching wall or ground.
  61. public bool isTouching()
  62. {
  63. if(isGround() || isWall())
  64. return true;
  65. else
  66. return false;
  67. }
  68.  
  69. //Returns direction of wall.
  70. public int wallDirection()
  71. {
  72. bool left = Physics2D.Raycast(new Vector2(player.transform.position.x - width, player.transform.position.y), -Vector2.right, length);
  73. bool right = Physics2D.Raycast(new Vector2(player.transform.position.x + width, player.transform.position.y), Vector2.right, length);
  74.  
  75. if(left)
  76. return -1;
  77. else if(right)
  78. return 1;
  79. else
  80. return 0;
  81. }
  82. }
  83.  
  84. //Feel free to tweak these values in the inspector to perfection. I prefer them private.
  85. public float speed = 14f;
  86. public float accel = 6f;
  87. public float airAccel = 3f;
  88. public float jump = 14f; //I could use the "speed" variable, but this is only coincidental in my case. Replace line 89 if you think otherwise.
  89.  
  90. private GroundState groundState;
  91.  
  92. void Start()
  93. {
  94. //Create an object to check if player is grounded or touching wall
  95. groundState = new GroundState(transform.gameObject);
  96. }
  97.  
  98. private Vector2 input;
  99.  
  100. void Update()
  101. {
  102. //Handle input
  103. if(Input.GetKey(KeyCode.LeftArrow))
  104. input.x = -1;
  105. else if(Input.GetKey(KeyCode.RightArrow))
  106. input.x = 1;
  107. else
  108. input.x = 0;
  109.  
  110. if(Input.GetKeyDown(KeyCode.Space))
  111. input.y = 1;
  112.  
  113. //Reverse player if going different direction
  114. transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, (input.x == 0) ? transform.localEulerAngles.y : (input.x + 1) * 90, transform.localEulerAngles.z);
  115. }
  116.  
  117. void FixedUpdate()
  118. {
  119. rigidbody2D.AddForce(new Vector2(((input.x * speed) - rigidbody2D.velocity.x) * (groundState.isGround() ? accel : airAccel), 0)); //Move player.
  120. rigidbody2D.velocity = new Vector2((input.x == 0 && groundState.isGround()) ? 0 : rigidbody2D.velocity.x, (input.y == 1 && groundState.isTouching()) ? jump : rigidbody2D.velocity.y); //Stop player if input.x is 0 (and grounded) and jump if input.y is 1
  121.  
  122. if(groundState.isWall() && !groundState.isGround() && input.y == 1)
  123. rigidbody2D.velocity = new Vector2(-groundState.wallDirection() * speed * 0.75f, rigidbody2D.velocity.y); //Add force negative to wall direction (with speed reduction)
  124.  
  125. input.y = 0;
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement