Advertisement
Guest User

asd

a guest
Jan 26th, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Snake : MonoBehaviour
  6. {
  7. // Start is called before the first frame update
  8. float StepSize;
  9. float timer = 0.0f;
  10. float GetDirectionInVerticalAxis;
  11.  
  12.  
  13. Vector2 StepForward;
  14. Vector2 InitialMove; //The direction of first move based on players choice
  15. Vector2 CurrentDirection;
  16.  
  17.  
  18. void Start()
  19. {
  20.  
  21. //Putting the snake in the center of the screen
  22. transform.position = Vector2.zero;
  23.  
  24.  
  25. //Gets the size of snake to calculate the size of step. It's a square so doesn't matter if it's y or x
  26. StepSize = transform.localScale.y;
  27.  
  28.  
  29.  
  30.  
  31. //Determines names of the keys pressed by player
  32.  
  33.  
  34.  
  35.  
  36.  
  37. float GetInitialDirectionInVerticallAxis = Input.GetAxis("Vertical"); //Checks which key is pressed and gives 1 or -1
  38. float GetInitialDirectionInHorizontalAxis = Input.GetAxis("Horizontal"); //Checks which key is pressed and gives 1 or -1
  39.  
  40.  
  41.  
  42. }
  43.  
  44. // Update is called once per frame
  45. void Update()
  46. {
  47.  
  48. //Determines the current move direction of snake and it's updated every second
  49. Vector2 DirectionInVerticalAxis;
  50.  
  51.  
  52.  
  53. //Code for steering in vertical axis
  54.  
  55.  
  56. GetDirectionInVerticalAxis = Input.GetAxis("Vertical"); //Checks which key is pressed and gives string 1 or -1
  57.  
  58.  
  59. if (GetDirectionInVerticalAxis==1 && StepForward.y !=-1) //Sets the directions of step based on the keys pressed
  60. {
  61. StepForward = new Vector2(0, StepSize);
  62. }
  63. else if (GetDirectionInVerticalAxis == -1 && StepForward.y != 1)
  64. {
  65. StepForward = new Vector2(0, -StepSize);
  66. }
  67.  
  68.  
  69.  
  70. //Code for steering in horizontal axis
  71. float GetDirectionInHorizontalAxis = Input.GetAxis("Horizontal"); //Checks which key is pressed and gives 1 or -1
  72.  
  73. if (GetDirectionInHorizontalAxis == 1 && StepForward.x != -1) //Sets the directions of step based on the keys pressed
  74. {
  75. StepForward = new Vector2(StepSize,0);
  76. }
  77. else if (GetDirectionInHorizontalAxis == -1 && StepForward.x != 1)
  78. {
  79. StepForward = new Vector2(-StepSize, -0);
  80. }
  81.  
  82.  
  83.  
  84. // Code for moving the snake. Timer counts to 1 second and then resets. Snake will move by 1 step every 1 second.
  85. timer += Time.deltaTime;
  86.  
  87.  
  88. if (timer >= 1f)
  89. {
  90.  
  91. transform.Translate(StepForward);
  92. timer = 0f;
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. }
  102.  
  103. //float move = 0f;
  104.  
  105.  
  106.  
  107.  
  108.  
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement