Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SantiN90 · Jan 22, 2011 at 12:32 AM · movementvector3basicsnake

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SantiN90 · Jan 22, 2011 at 07:14 AM

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 &gt; 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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image provintrails1 · Apr 05, 2014 at 04:22 AM 0
Share

Lol, feel bad for anyone who doesn't know spanish XD

avatar image cdm89 provintrails1 · Jul 02, 2016 at 05:26 PM 0
Share

yeah i have no idea what im looking at....guess its the thought that counts lol

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

MoveTowards inside Coroutine 2 Answers

Player isn't affected by AddForce(), Lerp() etc. 1 Answer

Greater than or equal to Vector3 1 Answer

Convert WASD to local rotation 1 Answer

Smooth translation 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges