Search Unity

I am remaking snake | movement issue

Discussion in '2D' started by jaden03, Feb 19, 2019.

Thread Status:
Not open for further replies.
  1. jaden03

    jaden03

    Joined:
    Nov 24, 2018
    Posts:
    9
    I am remaking snake, and I do not want you to be able to go backwards(Ex. you are going left and you can go right) this is my code

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         growthnumber = 0;
    4.         inverted = PlayerPrefs.GetString("Inverted");
    5.         growth = PlayerPrefs.GetInt("Growth");
    6.         speed = PlayerPrefs.GetFloat("Speed");
    7.  
    8.  
    9.         // Move the Snake every 100ms
    10.         InvokeRepeating("Move", speed, speed);
    11.  
    12.  
    13.         spawnfood.Spawn();
    14.     }
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (inverted == "False")
    4.         {
    5.             // Move in a new Direction?
    6.             if (Input.GetKeyDown(KeyCode.RightArrow) && dir != Vector2.left)
    7.                 dir = Vector2.right;
    8.             else if (Input.GetKeyDown(KeyCode.DownArrow) && dir != Vector2.up)
    9.                 dir = -Vector2.up;    // '-up' means 'down'
    10.             else if (Input.GetKeyDown(KeyCode.LeftArrow) && dir != Vector2.right)
    11.                 dir = -Vector2.right; // '-right' means 'left'
    12.             else if (Input.GetKeyDown(KeyCode.UpArrow) && dir != Vector2.down)
    13.                 dir = Vector2.up;
    14.         }
    On the update function I have
    && dir != Vector2.(direction)
    so if you are going left you cant go right, etc
    Here is my problem
    for example
    you are going left, if you press up and then right fast enough, then you go from left to right instantly without going up, and if you have a tail then you instantly lose.
     
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Well, you could set a bool variable isMovingLeft and isMovingUp.

    Code (CSharp):
    1. if(isMovingLeft == true)
    2. {
    3.   // Insert your left move code here
    4. // Probably best to design a function
    5. // and call the function here
    6. }
    7.  
    8. if(isMovingLeft == false) // !isMovingLeft
    9. {
    10.   // Insert right moving code here
    11. }
    Something like this would help prevent you from moving backwards. What you'd need to do is detect which button was pressed and assign the variables inside of that code.

    Code (CSharp):
    1. void Update()
    2. {
    3.    if(Input.GetKeyDown(Keycode.LeftArrow)
    4. {
    5.      isMovingLeft = true;
    6. }
    7.    if(Input.GetKeyDown(Keycode.RightArrow && isMovingLeft == false)
    8. {
    9.      isMovingLeft = false;
    10. }
    11. if(Input.GetKeyDown(Keycode.UpArrow && isMovingUp == false)
    12. {
    13.      isMovingLeft = false;
    14.      isMovingUp = true;
    15. }
    16.  
    17. if(isMovingLeft == true)
    18. {
    19.     MoveLeft();
    20. }
    21. if(isMovingLeft == false)
    22. {
    23.     MoveRight();
    24. }
    25. }// End of Update()
    26. void MoveLeft()
    27. {
    28.    Vector2.left;
    29. // You get the point without me having to get too deep in code
    30. }
    31. void MoveRight()
    32. {
    33.    Vector2.right;
    34. // You get the point without me having to get too deep in code
    35. }
    36.  
    I edited to demonstrate a little better for ya.
     
    Last edited: Feb 20, 2019
  3. trose3

    trose3

    Joined:
    Jan 5, 2023
    Posts:
    1
    I'm having trouble with my code as well, similar problem but different code.

    private Vector2 _direction = Vector2.right;
    private List<Transform> _segments = new List<Transform>();
    public Transform segmentPrefab;
    public int initialSize = 4;
    private void Start()
    {
    ResetState();
    }
    private void Update()
    {
    if (Input.GetKeyDown(KeyCode.W))
    {
    _direction = Vector2.up;
    } else if (Input.GetKeyDown(KeyCode.S))
    {
    _direction = Vector2.down;
    } else if (Input.GetKeyDown(KeyCode.A))
    {
    _direction = Vector2.left;
    } else if (Input.GetKeyDown(KeyCode.D))
    {
    _direction = Vector2.right;
    }
    }
    private void FixedUpdate()
    {
    for (int i = _segments.Count - 1; i > 0; i--)
    {
    _segments.position = _segments[i - 1].position;
    }
    this.transform.position = new Vector3(
    Mathf.Round(this.transform.position.x) + _direction.x,
    Mathf.Round(this.transform.position.y) + _direction.y,
    0.0f
    );
    }
    private void Grow()
    {
    Transform segment = Instantiate(this.segmentPrefab);
    segment.position = _segments[_segments.Count - 1].position;
    _segments.Add(segment);
    }
    private void ResetState()
    {
    for (int i = 1; i < _segments.Count; i++)
    {
    Destroy(_segments.gameObject);
    }
    _segments.Clear();
    _segments.Add(this.transform);
    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();
    }
    }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,749
    Then start a new post. Please do not necro-post.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
Thread Status:
Not open for further replies.