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
1
Question by MSten · Feb 05, 2014 at 09:31 PM · 2dmovementcharacter controllerincrementconstant

2D Character controller that moves in increments.

Hello,

I need some help with creating a controller for my 2D character. The problem I am having is that the character should move in a constant speed forward and then move to a specific Y-coordinate when a button is pressed.

In essence,
character moves forward in X with a constant speed.
If button is pressed, move character 3 units in Y while not losing speed in X.
If button is pressed again, move character another 3 units in Y from the new position. etc.

How could I do this in a neat way, I would like the Y-movement to be smooth, so using a lerp seems reasonable. The only thing I can accomplish myself is the movement in Y or X, not both at the same time.

Thanks in advance, Martin.

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
1
Best Answer

Answer by giano574 · Feb 05, 2014 at 11:10 PM

You can use Transform.Translate with a constant in the X-position. You can use it in a coroutine like this:

 IEnumerator moveCharacter()
     {
         while (transform.position.y < moveToThisY)
         {
             transform.Translate(xSpeed * Time.deltaTime, ySpeed * Time.deltaTime, 
                                 0);
     
             yield return null;
         }
         
         transform.Translate(xSpeed * Time.deltaTime, 0, 0);
     }

Then, each time the button is pressed increment the moveToThisY by 3. You should probably set the y position to this variable as well, to ensure that it is exactly right, like this.

 transform.position = new Vector3(transform.position.x, moveToThisY, 
                                  transform.position.z);

Place this line right after the while loop in the coroutine. You will also have to call the coroutine once in the Start function, like this:

 StartCoroutine(moveCharacter());

EDIT: Also note, that you should never actually use "transform" if you have to use it often. You should store a reference to it instead. In the class declare a variable:

 private Transform myTransform;

And in the Start function assign myTransform to transform:

 void Start()
 {
    myTransform = transform;
 }
Comment
Add comment · Show 6 · 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 giano574 · Feb 05, 2014 at 11:30 PM 1
Share

You could, of course do the exact same thing within the Update function, using an if statement ins$$anonymous$$d.

 void Update()
 {
     if (myTransform.position.y < moveToThisY)
     {
         myTransform.Translate(xSpeed * Time.deltaTime, ySpeed * Time.deltaTime, 
                               0);
     }
 
     else
     {
         myTransform.position = new Vector3(myTransform.position.x, 
                                            moveToThisY, myTransform.position.z);
         myTransform.Translate(xSpeed * Time.deltaTime, 0, 0);
     }
 }
avatar image MSten · Feb 06, 2014 at 08:05 AM 0
Share

Thank you, your solution works perfect, it even seems that the Transform.Translate gives better performance than moving rigidbodys.

Cheers!

avatar image giano574 · Feb 06, 2014 at 09:04 AM 0
Share

That's great. Did you use the coroutine method? I'm curious to know, since I am not sure that it works correctly.

avatar image MSten · Feb 06, 2014 at 04:15 PM 0
Share

I actually never got the coroutine to work to well with both X & Y, so I used the "if" way ins$$anonymous$$d.

avatar image giano574 · Feb 06, 2014 at 08:55 PM 0
Share

Okay. Looking back at it, I don't see any reason to use a coroutine anyway. And the way I made it was obviously wrong, as you say. But I'm glad it worked out for you.

Show more comments

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

19 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

Related Questions

2D Sprite leaves a trail when moving diagonally 1 Answer

How can I make 2D movement less jerky on a controller, with velocity and such? 0 Answers

wasd movement rigidbody no bouncing 0 Answers

Gravity switch 1 Answer

8 directional movement rotation problem 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