- Home /
Question by
Grimkick · Mar 23, 2017 at 11:39 AM ·
movementscorecoordinatescheckingcoordinate
Check Coordinate if its increasing by value over time
I try to make a score that is related to the distance my Character is moving. Its made in 2D. My Character ist moving at a constant speed and i want do add 1 to score the every time my Character moves 1 one the x coordinate forward(to the right). So far i only got a little bit of code where the score gets +1 on every FixedUpdate and that works but its not what i try do to. I thought on something with comparing transform.position but im not sure how to do that.
public Text scoreText;
public Vector3 position;
void Start()
{
score = 0;
}
void FixedUpdate()
{
if (isRunning)
{
Debug.Log("X-Coordinate = " + transform.position.x.ToString());
scoreText.text = score++.ToString();
}
}
Comment
Best Answer
Answer by Hellium · Mar 23, 2017 at 11:40 AM
This is how I would do it :
public Text scoreText;
public Vector3 position;
private int xPosition ;
void Start()
{
score = 0;
xPosition = Mathf.FloorToInt( transform.position.x ) ;
}
void FixedUpdate()
{
if (isRunning)
{
int x = Mathf.FloorToInt( transform.position.x ) ;
if( x > xPosition )
{
xPosition = x ;
scoreText.text = score++.ToString();
}
Debug.Log("X-Coordinate = " + transform.position.x.ToString());
}
}
Thats works realy good except for one thing, I watched at the Score and the Transform Component and its not 100% accurat but that is fine. So thanks for helping me.