Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by delfinatarga · Oct 25, 2017 at 04:48 AM · movementmovement script

Move an object to specific positions

Hello! i hope someone can help me with this. So i have an object that moves in the x axis with the arrow keys, but i want the object to go from one specific position to another when i press the arrow key, something like movement in lanes i guess? with four positions. A professor told me i should define four x positions (done) and add or subtract to get to those positions, but idk how to do that even tho it seems easy. This is my script:

 public class cube : MonoBehaviour {
     
     public float moveSpeed; 
     /*
     First position: -12.47
     Second position: -3.1
     Third position: 6.42
     Fourth position: 13.98
     */
 
     void Start () {
 
         moveSpeed = 10f;
     }
 
     void Update () {
         
         transform.Translate (moveSpeed*Input.GetAxis("Horizontal")*Time.deltaTime, 0f, 0f);
     }   
 }

 
Comment
Add comment · Show 1
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 Cuttlas-U · Oct 25, 2017 at 06:39 AM 0
Share

hi; try to paint or some how show me exactly what u want;

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by NickJVaccaro · Oct 25, 2017 at 02:46 PM

This would be a good use case for a Coroutine. If you're not familiar, a coroutine is basically a threaded function, so it can run on its own. The general flow of the code should be:

  1. Player presses an arrow key.

  2. The coroutine begins processing, moving the player from point A to point B.

  3. Once the coroutine has finished, the player stops moving and the script can now accept other key presses.

Your code would look something like this:

 public Vector3[] Positions;
 public float MoveDuration;
 
 private bool _canMove = true;
 private int _currentPosition = 0;
 
 void Update()
 {
   if(Input.GetAxis("Horizontal") && _canMove)
   {
     StartCoroutine(Move(Input.GetAxis("Horizontal") > 0));
   }
 }
 
 private IEnumerator Move(bool moveRight)
 {
   // Check if this is a valid move. If there is no lane to move into, just return.
   if(moveRight && _currentPosition + 1 < Positions.Length)
   {
     _currentPosition++;
   }
   else if(!moveRight && _currentPosition - 1 >= 0)
   {
     _currentPosition--;
   }
   else
   {
     return;
   }
 
   // Make sure the player can't move while switching lanes. Could cause all kinds of bugs otherwise.
   _canMove = false;
 
   // Now actually perform the move.
   float timer = 0;
   Vector3 startPosition = transform.position;
   Vector3 endPosition = Positions[_currentPosition];
   while (timer < MoveDuration)
   {
     timer += Time.deltaTime;
     transform.position = Vector3.Lerp (startPosition, endPosition, timer / MoveDuration);
     
     // In a coroutine, doing "yield return null" stops processing until the next frame.
     yield return null;
   }
   
   // Make sure you set their final position after the loop is done processing.
   transform.position = endPosition;
   
   // Make sure the player can move once again.
   _canMove = true;
 }

I'm not at my dev computer so can't verify that this is 100% correct, but hopefully it can get you along the right lines. Let me know if you have any questions, thanks!

Comment
Add comment · 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

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

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy shakes on Y-axis when is moving straight on the X-axis 0 Answers

How do I stop momentum in 2D sidescroller? 1 Answer

Mouse move object according to grid 3 Answers

How can I make a prefab object from a list to move?,How can I make an object from a list to move by itself? 2 Answers

My character moves when i dont want to 0 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