Hi, I was trying to make a Snake game, but I stumbled upon a little thing.
I'm not certain of what kind of Input I have to use. What I want to do is to move the "Head" in the direction I pressed the key (whichever: up, down, left, right) until whether another key is pressed, or the snake crashes something.
I tried Input.GetKey and Input.GetKeyDown, but the GameObject just moves randomly and it kinda shifts between old and new positions.
this is my motion code (which is located on the "Head" GameObject):
using UnityEngine;
using System.Collections;
public class SnakeHeadMotion : MonoBehaviour {
public Vector3 SnakePos;
public int XPos = 0;
public float SnakeMove = 1f;
public static bool MovingUp = false;
float SnakeSpeed = .5f;
// Use this for initialization
void Start () {
SnakePos = this.rigidbody.position;
MovingUp = false;
}
void ContinueUp() {
MovingUp = true;
StartCoroutine("UpPress");
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.UpArrow))
{
ContinueUp();
}
}
IEnumerator UpPress() {
while(MovingUp){
if (SnakePos.z < 10)
{
SnakePos = new Vector3(SnakePos.x, SnakePos.y, SnakePos.z - SnakeMove);
this.transform.position = SnakePos * Time.deltaTime;
}
yield return new WaitForSeconds(SnakeSpeed);
}
}
}
I know it's kind of a silly question but it's really troubling me.
The rest of the movements would be made based on that one.
If you have any questions related to the code itself, feel free to ask.
OK, I just figured it out.
I'll leave the result here so as to give this question some closure. :)
Here's how it went out:
using UnityEngine;
using System.Collections;
public class SnakeHeadMotion: MonoBehaviour {
public bool Mover_Arriva;
public bool Mover_Abajo;
public bool Mover_Derecha;
public bool Mover_Izquierda;
public Cuerpo primer_cuerpo;
public float Tiempo_movimiento = .5F;
public float Siguiente_movimiento;
// Use this for initialization
void Start () {
Mover_Arriva = false;
Mover_Abajo = false;
Mover_Derecha = false;
Mover_Izquierda= false;
Siguiente_movimiento = Time.time + Tiempo_movimiento;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Mover_Arriva = true;
Mover_Abajo = false;
Mover_Derecha = false;
Mover_Izquierda = false;
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
Mover_Arriva = false;
Mover_Abajo = true;
Mover_Derecha = false;
Mover_Izquierda = false;
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
Mover_Arriva = false;
Mover_Abajo = false;
Mover_Derecha = true;
Mover_Izquierda = false;
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
Mover_Arriva = false;
Mover_Abajo = false;
Mover_Derecha = false;
Mover_Izquierda = true;
}
if (Time.time > Siguiente_movimiento)
{
MoverCabeza();
}
}
void MoverCabeza()
{
if (Mover_Arriva)
{
primer_cuerpo.mover(this.transform);
this.transform.position += transform.forward *transform.localScale.z;
}
if (Mover_Abajo)
{
primer_cuerpo.mover(this.transform);
this.transform.position += -transform.forward * transform.localScale.z;
}
if (Mover_Derecha)
{
primer_cuerpo.mover(this.transform);
this.transform.position += transform.right * transform.localScale.z;
}
if (Mover_Izquierda)
{
primer_cuerpo.mover(this.transform);
this.transform.position += -transform.right * transform.localScale.z;
}
Siguiente_movimiento = Time.time + Tiempo_movimiento;
}
}
I just tweaked a few things and added all the other directions. Hope it helps future programmers.