- Home /
Endless runner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMotor_2 : MonoBehaviour
{
public int Direction;
void Start()
{
}
void Update(){
Analyze ();
if (Input.GetKeyDown (KeyCode.D)) {
print ("left");
if (Direction == 0 || Direction == 1) {
Direction--;
}
} else if (Input.GetKeyDown (KeyCode.A)) {
print ("right");
if (Direction == 0 || Direction == -1) {
Direction++;
}
}
transform.Translate(5f * Time.deltaTime, 0f, 0f);
}
void Analyze(){
transform.position = Vector3.Lerp (transform.position, new Vector3 (0, 0, 3 * Direction), 0.1f);
}
}
I managed to move the character to the right and to the left but I can not make it go on endlessly
Answer by tanoshimi · Jul 21, 2017 at 05:16 PM
There's a lot of issues here, but let's start with the basics - why are you mapping "D" to "left" and "A" to "right"? Your character will never go endlessly to the left or right - the maximum they will go is 3 * Direction (line 35), and Direction will never have an absolute value greater than 1 due the logic in lines 21-26 - so I'd expect it to go from -3 to +3.
Is it fair but can I solve all this? $$anonymous$$y aim was for the character to go forward infinitely and move on three lanes
Answer by spigo98 · Jul 21, 2017 at 05:26 PM
Is it fair but can I solve all this? My aim was for the character to go forward infinitely and move on three lanes