x


Make GameObject move in one direction continuously on key press on C#

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.

more ▼

asked Jan 22 '11 at 12:32 AM

SantiN90 gravatar image

SantiN90
20 4 5 11

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jan 22 '11 at 07:14 AM

SantiN90 gravatar image

SantiN90
20 4 5 11

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1368
x578
x76
x29

asked: Jan 22 '11 at 12:32 AM

Seen: 5755 times

Last Updated: Jan 22 '11 at 12:32 AM