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 gane1404 · Jul 24, 2021 at 09:30 PM · updatetransform.translateswitch-case

Help me to understand how update and switch work in loop together.

Hello! I've started learning unity week ago and right now i'm doing a little project. I want to make a 3 line movement game (like Subway surfers etc.) and after a lot of troubleshooting and googling i did manage to get the movement result that i wanted. Here is the code:

 public class PlayerControllerMoveTo : MonoBehaviour
 {
     private int lane = 1;
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.D) && lane < 2)
         {
             lane++;
         }
         if (Input.GetKeyDown(KeyCode.A) && lane > 0)
         {
             lane--;
         }
 
         Move(lane);
     }
 
     private void Move(int lane)
     {
         Vector3 rightPos = new Vector3(3, 1.5f, transform.position.z);
         Vector3 leftPos = new Vector3(-3, 1.5f, transform.position.z);
         Vector3 middlePos = new Vector3(0, 1.5f, transform.position.z);
         float speed = 10f;
         float step = speed * Time.deltaTime;
         
         transform.Translate(Vector3.forward * Time.deltaTime * speed);
 
         switch (lane)
         {
             case 0:
                 transform.Translate(Vector3.forward * Time.deltaTime * speed);
                 transform.position = Vector3.MoveTowards(transform.position, leftPos, step);
                 break;
             case 1:
                 transform.Translate(Vector3.forward * Time.deltaTime * speed);
                 transform.position = Vector3.MoveTowards(transform.position, middlePos, step);
                 break;
             case 2:
                 transform.Translate(Vector3.forward * Time.deltaTime * speed);
                 transform.position = Vector3.MoveTowards(transform.position, rightPos, step);
                 break;
         }
     }
 }

However, i actually don't understand how code that i've written works. For some reason, if i take out "transform.Translate(Vector3.forward Time.deltaTime speed);" either from switch cases or from before the switch happens, my player only moves forward for a bit while it moves when switching lanes, but not on its own like i want it to.
Help me understand the flaw in my logic. Update function should be called constantly, so it should always go through the entire code that i've written, thus it means it should always move forward z and always stay on the specified x until lane switches, right? Then why do i need to call my forward statement twice in order for the movement to work?

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 gane1404 · Aug 07, 2021 at 09:44 PM

I see there is like 126 people following the question, not really sure how common it is here, but i guess i'll answer my question myself, hopefully it might help someone. What i'm assiming happening is move towards locks player when a single translate happens, so thats why i managed to make player move with 2 translates. However, solution that i posted is just outright bad because when player switches lanes using this script, player object actually speeds up twice during switch. Now, i have implemented a new way to move forward by using rigidbody instead and tested the player object moving speed by doing some precise caluclations with other object on my scene and it seems this way is bug-free.

 private void Move(int lane)
     {
         Vector3 rightPos = new Vector3(3, 1.5f, transform.position.z);
         Vector3 leftPos = new Vector3(-3, 1.5f, transform.position.z);
         Vector3 middlePos = new Vector3(0, 1.5f, transform.position.z);
 
         Vector3 playerVelocity = new Vector3(0, 0, speed);
         
         float step = speed * Time.deltaTime;
         
         playerRb.velocity = playerVelocity;
 
         switch (lane)
         {
             case 0:
                 transform.position = Vector3.MoveTowards(transform.position, leftPos, step);
                 break;
 
             case 1:
                 transform.position = Vector3.MoveTowards(transform.position, middlePos, step);
                 break;
 
             case 2:
                 transform.position = Vector3.MoveTowards(transform.position, rightPos, step); 
                 break;
         }
 }

However, if you are making an infinite runner game you are better off making scene move towards the player instead (or implement some kind of invisible player position reset), because you dont want player to end up in faraway coordinates.

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

126 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 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

Switch statement not running on every frame 1 Answer

Script wait for 3 seconds before running - stops it working altogether 2 Answers

Is there a yield WaitForSeconds type code for the update? 2 Answers

Problem with animation and void fixedupdate() 1 Answer

My Script Won't Update 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