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 /
  • Help Room /
avatar image
0
Question by FelixKu · Feb 25, 2020 at 09:17 PM · movementcar game

How to make player move from one track to the other gradually?

I am making a game that contains several tracks, a player car and each track is labeled with a specific button on the keyboard. I know how to change the position of the car from one track to the other but is there a way to make the car move gradually from one track to the other (like a normal car does), instead of instantly changing its position to another track?

For example, the tracks are labeled with "A", "S", "D", when I press "A" on the keyboard, the car instantly switch its position to track "A" but I would like it to be moving from the current track gradually to the "A" track.

Thank you for the help in advance!!

Comment
Add comment · Show 3
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 spencerz · Feb 26, 2020 at 02:20 AM 0
Share

Can you post your code?

avatar image FelixKu · Feb 26, 2020 at 03:05 AM 0
Share

Here is my code for now, I am collecting input into variable "temp" and switch the user to different locations by a "switch" statement. The cases in the switch-statement are my special inputs but I am wondering how to make the player move from one position to another gradually with movements ins$$anonymous$$d of instantly changing position like $$anonymous$$e.

I have also tried "transform.position = Vector3.$$anonymous$$oveTowards(transform.position, move, Time.deltaTime * speed);" inside switch cases but it doesn't work very well. For example, when "32" is recorded in "temp", it just move a bit by a bit everytime "32" is recorded ins$$anonymous$$d of recording once->move automatically to the specific location.

Thanks

avatar image FelixKu · Feb 26, 2020 at 03:06 AM 0
Share
  Vector3 move = transform.position;
              switch (temp)
              {
                  case "3":
                      Debug.Log("30");
                      move = new Vector3(-15.37f, move.y, move.z);
                      transform.position = move;
                      break;
                  case "32":
                      Debug.Log("32");
                      move = new Vector3(-13.24f, move.y, move.z);
                      transform.position = move;
                      break;
                  case "34":
                      Debug.Log("34");
                      move = new Vector3(-10.39f, move.y, move.z);
                      transform.position = move;
                      break;
                  case "35":
                      Debug.Log("35");
                      move = new Vector3(-8.12f, move.y, move.z);
                      transform.position = move;
                      break;
                  default:
                      Debug.Log("NO INPUT");
                      break;
              }

1 Reply

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

Answer by spencerz · Feb 26, 2020 at 02:09 PM

I know you said you tried "moveTowards" but I gave it shot and this code is working pretty smooth for me. i attached it to my "car" gameObject. I tested this in 2D which is why i used a Vector2.MoveTowards(, just change to vector 3 if youre doing a 3d game.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class JustTesting : MonoBehaviour
 {
     public Transform road1, road2, road3, road4;
 
     public float speed = 1;
 
     public string temp;
     // Start is called before the first frame update
     void Update()
     {
         Switch();
         
         if (Input.GetKey (KeyCode.A))
         {
             temp = "3";
         }
         if (Input.GetKey(KeyCode.S))
         {
             temp = "32";
         }
         if (Input.GetKey(KeyCode.D))
         {
             temp = "34";
         }
         if (Input.GetKey(KeyCode.W))
         {
             temp = "35";
         }
     }
 
     // Update is called once per frame
     void Switch()
     {
 
         Vector3 move;
         float step = speed * Time.deltaTime;
 
         switch (temp)
         {
             case "3":
                 Debug.Log("30");
 
                 move  = Vector2.MoveTowards(transform.position, road1.position, step);
                 transform.position = move;
                 break;
 
             case "32":
                 Debug.Log("32");
                 move = Vector2.MoveTowards(transform.position, road2.position, step);
                 transform.position = move;
 
                 break;
             case "34":
                 Debug.Log("34");
                 move = Vector2.MoveTowards(transform.position, road3.position, step);
                 transform.position = move;
 
                 break;
             case "35":
                 Debug.Log("35");
                 //move = road4.position;
                 //transform.position = move;
                 break;
             default:
                 Debug.Log("NO INPUT");
                 break;
         }
 
     }
 }

alt text


ezgifcom-gif-maker-2.gif (494.6 kB)
Comment
Add comment · Show 4 · 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 FelixKu · Feb 27, 2020 at 11:26 AM 0
Share

Thank you so much for the codes and recreating my scene! Sorry for not giving enough detail of my game, I am actually having the player to move constantly towards the front ins$$anonymous$$d of moving the road image, so when I use your code above, it still moves a bit by a bit every time I pressed the button.

I am wondering maybe the problem is because of the continuously changing "z" axis value of my player.

I am now trying the following code where "z" is added by some constant value times the speed to get the roughly moved location, but it also moves a bit by a bit...

 move = Vector3.$$anonymous$$oveTowards(transform.position, new Vector3(-15.37f, transform.position.y, transform.position.z+30*speed) , step);

Thank you for the help and also the gif capture!!

Below is a picture of my desired outcome.

alt text

繪圖.png (63.1 kB)
avatar image spencerz FelixKu · Feb 27, 2020 at 02:59 PM 0
Share

no sweat i enjoy stuff like this. im trying to understand the concept of the game because im confused. from what i gather. you want to gradually move the car from its current lane to another lane while also moving it forward? thats easy (in 2d)

     case "3":
         Debug.Log("3");

         move = Vector3.$$anonymous$$oveTowards(transform.position, new 
          Vector3(road1.transform.position.x, road1.transform.position.y + 3, 
          road1.transform.position.z ), step);
         transform.position = move;
         
         break;

that codet^ moves the car to road one's transform position plus 3f on the y which replicates exactly what you drew for me. so im kinda confused on what the concept of the game is or the exact objective here

avatar image FelixKu spencerz · Feb 28, 2020 at 02:36 AM 1
Share

Thank you very much!! The program can move exactly the way I want now! Actually I am making a game that makes use of $$anonymous$$IDI devices so I am referring the specific $$anonymous$$IDI signals to the specific roads the player car is moving. (To let player press the right key on their $$anonymous$$IDI devices to move to the correct road and dodge the obstacles on the roads).

I also found out that the problem why "movetowards" and other functions like "translate" does not work is because I placed a temp="" at the beginning of Update(). Because I am receiving the $$anonymous$$IDI signals into "temp", I thought I need to set it back to null but it turns out that this is why the problem lied. (Sorry for not giving the full codes and clear picture of my program).

Have a nice day!

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

255 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 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 avatar image avatar image avatar image

Related Questions

Simple arcade vehicle turning with or w/o wheel colliders 0 Answers

how do i stop unity from keeping my button pressed? 0 Answers

Weird behaviour with WASD and arrow keys 1 Answer

can i change this into UI controls ?? 0 Answers

Simple vehicle script 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