Question by
Nagamorii · Mar 21, 2019 at 03:08 AM ·
scripting problem
Player movement help
Is there any script that moves my player to X place? Like i press A and it goes 3 blocks to Left, i press D and goes 3 blocks to the right, like this game. https://www.youtube.com/watch?v=E1_DZsgyZh0
I've been doing my research but couldnt find anything.
Comment
Answer by Vollmondum · Mar 21, 2019 at 07:26 AM
public int currentPlayerPos; // index of current lane. 0 for center
public Vector2 playerPosLimits; // enter left/right position limits here
public float playerPos[]; // enter coordinates of lanes, left to right
public float posChangeSpeed;
void Update()
{
if(Input.GetKeyUp(KeyCode.D))
{
if(currentPlayerPos < playerPosLimits.y)
{
currentPlayerPos++;
}
}
if(Input.GetKeyUp(KeyCode.A))
{
if(currentPlayerPos > playerPosLimits.x)
{
currentPlayerPos--;
}
}
if(transform.position.x != playerPos[currentPlayerPos])
{
Vector3 newPosition = new Vector3(playerPos[currentPlayerPos], transform.position.y, transform.position.z);
transform.position = Vector3.MoveTowards(transform.position, newPosition, posChangeSpeed * Time.deltaTime);
}
}
Your answer
Follow this Question
Related Questions
Javascript Movement too slow or fast. 1 Answer
error CS1525: Unexpected symbol `(', expecting `,', `;', or `=' 1 Answer
I cant get my animator to play when injuried or dead. please help 0 Answers
Null Reference Exception on an unused script 1 Answer
Using a button to set a Script as Active or Inactive 0 Answers