Right hello, so I am trying to recreate snake as a way to procrastinate my time away and I have ran into an issue with my code, it shows no errors but when I change the public value of an int it just seems to stop working…
If you could help me figure out why I would be grateful, also any other tips you can offer would be a great, thanks in advance.
Code
using UnityEngine;
using System.Collections;
public class SnakeMove : MonoBehaviour {
public int SnakeDirection;
public Rigidbody2D Snake;
public Vector2 SnakeMoveDirection;
public int SecondsBetweenMove;
private int MoveTimer;
private int MoveTimerStatic;
// Use this for initialization
void Start () {
Snake = GetComponent<Rigidbody2D>();
MoveTimerStatic = (SecondsBetweenMove*60);
if (SecondsBetweenMove == 0) {
SecondsBetweenMove = 10;
}
}
// Update is called once per frame
void Update (){
SnakeFacing ();
SnakeTravel ();
SnakeMoveFace ();
MoveTimer = (MoveTimer - 1);
}
void SnakeTravel () {
if (SnakeDirection == 1) {
SnakeMoveDirection.x = 5;
SnakeMoveDirection.y = 0;
}
if (SnakeDirection == 0) {
SnakeMoveDirection.x = -5;
SnakeMoveDirection.y = 0;
}
if (SnakeDirection == 3) {
SnakeMoveDirection.y = 5;
SnakeMoveDirection.x = 0;
}
if (SnakeDirection == 4) {
SnakeMoveDirection.y = -5;
SnakeMoveDirection.x = 0;
}
}
void SnakeMoveFace (){
if (MoveTimer < 1){
MoveTimer = MoveTimerStatic;
Snake.MovePosition(Snake.position+SnakeMoveDirection);
}
}
void SnakeFacing () {
if (Input.GetAxisRaw("Horizontal")>0){
if (SnakeDirection == 1|| SnakeDirection == 0){}else{
SnakeDirection = 1;
//right
Debug.Log("right 1");}
}
if (Input.GetAxisRaw("Horizontal")<0){
if (SnakeDirection == 1|| SnakeDirection == 0){}else{
SnakeDirection = 0;
//left
Debug.Log("left 0");}
}
if (Input.GetAxisRaw ("Vertical") > 0) {
if (SnakeDirection == 4|| SnakeDirection == 3) {}else{
SnakeDirection = 3;
//up
Debug.Log ("up 3");
}
}
if (Input.GetAxisRaw ("Vertical") < 0) {
if (SnakeDirection == 3|| SnakeDirection == 4) {}else{
SnakeDirection = 4;
//down
Debug.Log ("down 4");
}
}
}
}