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
0
Question by Gamesatan · May 29, 2017 at 08:44 PM · 2dmovement

Moving player between pre-determined points

Hello!

I'm new to unity and have some questions for you guys.

I'm creating a 2d game in which you move a rabbit between 3 lanes in order to avoid falling blocks. I already have a character with animation and collision standing on a sprite with a box collider that acts as ground.

So basically i would like to know how to move my rabbit left and right between 3 points using arrow keys. I found plenty of tutorials online in how to make a character move around, but nothing regarding this. Or maybe i just don't know what to google.

Here's a picture to further illustrate my problem since my english isn't the best. https://up.ppy.sh/files/721341236123145.png

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

Answer by ShadyProductions · May 29, 2017 at 08:44 PM

You can do something like this:

 public Vector3 leftPos;
 public Vector3 rightPos;
 public Vector3 centralPos;

 void Start()
 {
     //Start in the center position
     transform.position = centralPos;
 }

 void Update()
 {
     //move to the correct position
     var posToMove = DeterminePos(transform.position);
     if (posToMove != null)
         transform.position = (Vector3)posToMove;
 }

 private Vector3? DeterminePos(Vector3 pos)
 {
     if (Input.GetKeyDown(KeyCode.LeftArrow))
     {
         //check if we are in the outer left
         if (transform.position == leftPos)
             return leftPos; //return same position because we don't need to move
         if (transform.position == centralPos)
             return leftPos; //we want to move to the outer left position
         if (transform.position == rightPos)
             return centralPos; //we want to move to the center position
     }

     if (Input.GetKeyDown(KeyCode.RightArrow))
     {
         //check if we are in the outer left
         if (transform.position == rightPos)
             return rightPos; //return same position because we don't need to move
         if (transform.position == centralPos)
             return rightPos; //we want to move to the outer right position
         if (transform.position == leftPos)
             return centralPos; //we want to move to the center position
     }
     return null; //default
 }

Comment
Add comment · Show 3 · 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 paezrice · May 29, 2017 at 09:35 PM 0
Share

you might wanna add a kind of lerp to this solution, i guess. i think it would "warp" the character between the lanes like this, right?

avatar image ShadyProductions paezrice · May 30, 2017 at 06:50 AM 0
Share

Yes it will practically just teleport the character between the lanes.

avatar image Gamesatan ShadyProductions · May 30, 2017 at 02:41 PM 0
Share

Looks like a solid plan, however i was thinking of having movement animation so i assume warping wont work there? Someone suggested on the forums adding 3 trigger on the ground that stop all movement, and then setting the velocity of the rigidbody to move with the right speed & in the desired direction until it collides with the trigger.

I think i'll try that out first, even though i have no idea how to script it yet. :p

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Player movement boudaries in 2D 1 Answer

Uneven speed in 2d movement script 2 Answers

How can I make a collision sensitive teleporter for a 2D game #c 1 Answer

Absolute character movement and collision 0 Answers

Rigidbody2D x velocity not moving unless placed in FixedUpdate 1 Answer


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