0
\$\begingroup\$

Saw a thread with the same issue, but didn't seem like the question was answered: Snake Game body rotation

As shown in the above thread, I have a Snake game that I'm developing for a project. I have made two sprites: "SnakeBodyVertical.png" and "SnakeBodyHorizontal.png".

I wish to swap between these two sprites for each body sprite of the snake object depending on if the body part is moving in a horizontal direction, or vertical direction. But I'm unsure of how to do this.

My Current Snake Movement Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Snake : MonoBehaviour
{

    private Vector2 _direction = Vector2.right;
    private List<Transform> _segments = new List<Transform>();
    private KeyCode lastHitKey = KeyCode.D;

    public Transform segmentPrefab;
    public int initialSize = 3;

    public Sprite SnakeHeadUp;
    public Sprite SnakeHeadRight;
    public Sprite SnakeHeadLeft;
    public Sprite SnakeHeadDown;

    private void Start()
    {
        ResetState();
    }


    // Update: Updated Once Per Time (Edit -> Project Settings -> Time (0.09) to change difficulty!)
    // This function is controlling the direction our snake moves.
    void Update()
    {

        if(Input.GetKeyDown(KeyCode.W) && lastHitKey != KeyCode.S)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = SnakeHeadUp;
            lastHitKey = KeyCode.W;
            _direction = Vector2.up; 

        } else if (Input.GetKeyDown(KeyCode.S) && lastHitKey != KeyCode.W)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = SnakeHeadDown;
            lastHitKey = KeyCode.S;
            _direction = Vector2.down;
            
        } else if (Input.GetKeyDown(KeyCode.A) && lastHitKey != KeyCode.D)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = SnakeHeadLeft;
            lastHitKey = KeyCode.A;
            _direction = Vector2.left;

        } else if(Input.GetKeyDown(KeyCode.D) && lastHitKey != KeyCode.A)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = SnakeHeadRight;
            lastHitKey = KeyCode.D;
            _direction = Vector2.right;
        }
    }

    //This transforms the position of our snake head, rounded to a whole number. This should force our snake to move in a grid.
    private void FixedUpdate()
    {
        for (int i = _segments.Count - 1; i > 0; i--){
            _segments[i].position = _segments[i - 1].position;
            _segments[i].rotation = _segments[i - 1].rotation;
            Debug.Log(_segments[i].rotation);
        }
    
        this.transform.position = new Vector3(
            Mathf.Round(this.transform.position.x) + _direction.x,
            Mathf.Round(this.transform.position.y) + _direction.y,
            0.0f
        );
    }

    //Function handles the logic of iterating in reverse from a list of prefabs of our snake segments, to create a new snake segment.
    private void Grow()
    {
        Transform segment = Instantiate(this.segmentPrefab);
        segment.position = _segments[_segments.Count - 1].position; //from the list of our segments, get the very last segment (-1) 

        _segments.Add(segment);
    }

    //Function handles the logic of resetting the state of our game upon collision with objects other than food
    private void ResetState(){

        //emptying the snakelist
        for(int i = 1; i < _segments.Count; i++)
        {
            Destroy(_segments[i].gameObject);
        }

        _segments.Clear();
        _segments.Add(this.transform);

        //resetting our snake from 1 back to initialsize (3)
        for(int i = 1; i < this.initialSize; i++)
        {
            _segments.Add(Instantiate(this.segmentPrefab));
        }

        this.transform.position = Vector3.zero;
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "Food"){
            Grow();
        } else if (other.tag == "Obstacle"){
            ResetState();
        }
    }
}

I would like any recommendations on where to find content to answer this question, and or advice on solving this issue. I'm relatively new to coding so be nice :)

Thank You!

\$\endgroup\$

0

You must log in to answer this question.

Browse other questions tagged .