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!!
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
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;
}
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;
}
}
}
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.
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
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!
Your answer
Follow this Question
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