Eyrik
Eyrik

Reputation: 127

Tracking distance. Unity 2D game

If I have a object that starts at 0 on the x axis, and moving it in the x+ axis. Is there a way I can track that distance made, and use it counter?

I have no clue what im doing, but was thinking something like

var distance = orgpos -> currentpos;
for each x10{
score += 1;  } 

And this action performed live while moving.

Edit:

calculatedDistance += (transform.position - previousPosition).magnitude;
previousPosition = transform.position;

I have this script that give me the distance, if thats to any help.

Upvotes: 1

Views: 2701

Answers (1)

Eyrik
Eyrik

Reputation: 127

Found a way after I posted the question. Ill just post the way I did it for others to use.

//Score
static var score : int = 0; 
static var distanceScore : int = 0; 
static var starScore : int = 0;

//Adding point and removing star for each collision
function OnCollisionEnter(collision : Collision)
{
if (collision.gameObject.tag == "StarPickup")
{
    Destroy(collision.gameObject);
    starScore += 10;   
}}    

// Checking distance
var previousPosition : Vector3; 
var calculatedDistance : float;

function Awake()
{
previousPosition = transform.position;
}

function Update()
{
calculatedDistance += (transform.position - previousPosition).magnitude;
previousPosition = transform.position;
distanceScore = Mathf.Round(calculatedDistance/10);   
score = distanceScore + starScore;
print("The score is: " + score);
}

Upvotes: 1

Related Questions