Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Snake : MonoBehaviour
- {
- // Start is called before the first frame update
- float StepSize;
- float timer = 0.0f;
- float GetDirectionInVerticalAxis;
- Vector2 StepForward;
- Vector2 InitialMove; //The direction of first move based on players choice
- Vector2 CurrentDirection;
- void Start()
- {
- //Putting the snake in the center of the screen
- transform.position = Vector2.zero;
- //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
- StepSize = transform.localScale.y;
- //Determines names of the keys pressed by player
- float GetInitialDirectionInVerticallAxis = Input.GetAxis("Vertical"); //Checks which key is pressed and gives 1 or -1
- float GetInitialDirectionInHorizontalAxis = Input.GetAxis("Horizontal"); //Checks which key is pressed and gives 1 or -1
- }
- // Update is called once per frame
- void Update()
- {
- //Determines the current move direction of snake and it's updated every second
- Vector2 DirectionInVerticalAxis;
- //Code for steering in vertical axis
- GetDirectionInVerticalAxis = Input.GetAxis("Vertical"); //Checks which key is pressed and gives string 1 or -1
- if (GetDirectionInVerticalAxis==1 && StepForward.y !=-1) //Sets the directions of step based on the keys pressed
- {
- StepForward = new Vector2(0, StepSize);
- }
- else if (GetDirectionInVerticalAxis == -1 && StepForward.y != 1)
- {
- StepForward = new Vector2(0, -StepSize);
- }
- //Code for steering in horizontal axis
- float GetDirectionInHorizontalAxis = Input.GetAxis("Horizontal"); //Checks which key is pressed and gives 1 or -1
- if (GetDirectionInHorizontalAxis == 1 && StepForward.x != -1) //Sets the directions of step based on the keys pressed
- {
- StepForward = new Vector2(StepSize,0);
- }
- else if (GetDirectionInHorizontalAxis == -1 && StepForward.x != 1)
- {
- StepForward = new Vector2(-StepSize, -0);
- }
- // Code for moving the snake. Timer counts to 1 second and then resets. Snake will move by 1 step every 1 second.
- timer += Time.deltaTime;
- if (timer >= 1f)
- {
- transform.Translate(StepForward);
- timer = 0f;
- }
- //float move = 0f;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement