Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class forD : MonoBehaviour
  6. {
  7. public Rigidbody2D rb2d;
  8. public float playerSpeed =5;
  9. public float jumpPower= 5;
  10. public int directionInput;
  11. public bool groundCheck;
  12. public bool facingRight = true;
  13.  
  14.  
  15.  
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. rb2d = GetComponent<Rigidbody2D>();
  20. }
  21.  
  22. // Update is called once per frame
  23.  
  24. void Update()
  25. {
  26.  
  27. if (Input.GetKey(KeyCode.Space))
  28. Jump();
  29.  
  30. float moveX = Input.GetAxis("Horizontal");
  31. if (Input.GetKey(KeyCode.D))
  32. {
  33. Move(1);
  34. }
  35.  
  36. else if (Input.GetKey(KeyCode.A))
  37. {
  38.  
  39. Move(-1);
  40. }
  41. else { Move(0); };
  42. if ((directionInput < 0) && (facingRight))
  43. {
  44. Flip();
  45. }
  46.  
  47. if ((directionInput > 0) && (!facingRight))
  48. {
  49. Flip();
  50. }
  51.  
  52. }
  53. void OnCollisionEnter2D(Collision2D col)
  54. {
  55. if (col.gameObject.tag == "gnd")
  56. {
  57. groundCheck = true;
  58. }
  59.  
  60. }
  61. void FixedUpdate()
  62. {
  63. rb2d.velocity = new Vector2(playerSpeed * directionInput, rb2d.velocity.y);
  64. }
  65.  
  66. public void Move(int InputAxis)
  67. {
  68.  
  69. directionInput = InputAxis;
  70.  
  71. }
  72.  
  73. public void Jump()
  74. {
  75.  
  76. if (groundCheck)
  77. {
  78. rb2d.velocity = new Vector2(rb2d.velocity.x, jumpPower);
  79. }
  80. groundCheck = false;
  81. }
  82.  
  83. void Flip()
  84. {
  85. facingRight = !facingRight;
  86. Vector3 theScale = transform.localScale;
  87. theScale.x *= -1;
  88. transform.localScale = theScale;
  89. }
  90.  
  91.  
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement