Hi everyone, I have the following code for a simple snake-like game
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Snake : MonoBehaviour
{
Vector2 dir = Vector2.right;
List<Transform> body = new List<Transform>();
bool growing = false;
public GameObject bodyPrefab;
private SpriteRenderer sr;
void Start()
{
InvokeRepeating("Move", 0.3f, 0.3f);
sr= GetComponent<SpriteRenderer>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
dir = Vector2.right;
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
dir = -Vector2.up;
;
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
dir = -Vector2.right;
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
dir = Vector2.up;
}
}
void Move()
{
if(dir==Vector2.right)
{
sr.flipX = false;
}
else if(dir==-Vector2.right)
{
sr.flipX = true;
}
else if(dir==Vector2.up)
{
transform.Rotate(0,0,90);
}
else
{
transform.Rotate(0,0,270);
}
}
By flipping the sprite I get the head to flip direction but when I use the rotate function it keeps rotating every time it invokes the function. What do I use to just rotate the sprite once?
Thanks!