- Home /
Unity C# - Snake Movment
This is my code
// Update is called once per frame
void Update () {
if( Input.GetAxisRaw("Vertical") > 0)//up
{
if(direction == 1)
{
}
else
{
direction = 1;
Debug.Log("up");
}
}
if( Input.GetAxisRaw("Vertical") < 0)//down
{
if(direction == 2)
{
}
else
{
direction = 2;
Debug.Log("down");
}
}
if( Input.GetAxisRaw("Horizontal") > 0)//left
{
if(direction == 3)
{
}
else
{
direction = 3;
Debug.Log("left");
}
}
if( Input.GetAxisRaw("Horizontal") < 0)//right
{
if(direction == 4)
{
}
else
{
direction = 4;
Debug.Log("right");
}
}
if(direction == 1)//up
{
transform.position = new Vector2(transform.position.x, transform.position.y + speed * Time.deltaTime);
}
if(direction == 2)//down
{
transform.position = new Vector2(transform.position.x, transform.position.y - speed * Time.deltaTime);
}
if(direction == 3)//left
{
transform.position = new Vector2(transform.position.x + speed * Time.deltaTime, transform.position.y);
}
if(direction == 4)//right
{
transform.position = new Vector2(transform.position.x - speed * Time.deltaTime, transform.position.y);
}
}
I Almost got it to work, I just have a problem when 2 keys are pressed at the same time, when they both pressed both numbers go up, I need the movement to act like, for example, I am holding down the down button, it need to be counted as one press not a continuous press, can anyone please help?
Comment
Answer by applemaniac · Jun 02, 2015 at 05:50 AM
i'd delete your code and try that :
if(input.getAxis("Vertical")!=0)
{
transform.position = new Vector2(transform.position.x + input.getAxis("Vertical")* speed * Time.deltaTime, transform.position.y);
}
else
{
transform.position = new Vector2(transform.position.x, transform.position.y + input.getAxis("Horizontal")* speed * Time.deltaTime);
}