- Home /
distance travelled on y axis
Making a game similar to doodle jump, and i would like to make a distance system as the score, but in my app is seems to be going really slow, you have to jump like 1-2 platforms to get to 1, while in doodle jump it takes 1-2 platforms to get to something like 100 -1000 also my current system counts the distance from a point even if the player moved to the side it will add 1 point even although the ball didn't actually jumped up.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class DistanceSystem : MonoBehaviour {
public float distance;
public Transform player;
public Text label;
float distanceTravelled = 0;
Vector3 lastPosition;
void Start () {
lastPosition = transform.position;
}
// Update is called once per frame
void Update () {
distanceTravelled += Vector3.Distance(transform.position, lastPosition);
lastPosition = transform.position;
label.text = distanceTravelled.ToString ("F0");
}
}
Why not always start at world position y=0? Then max height is just the max transform.position.y... No extra calculations required.
Answer by lordlycastle · Mar 28, 2015 at 09:11 PM
You need the vertical distance only, so you have to make the x and z attributes of Vectors to 0. Before measuring the distance, simply create new variable which are equal to Vector.Up
times movement. This’ll make sure that only vertical movement is measured in the Vector3.Distace
.
Answer by bishwasbhedu · May 08, 2021 at 02:54 PM
Perhaps this is a better solution brother. You just need to subtract your transforum.postion.z
with your first ever position.
Your answer
Follow this Question
Related Questions
Find out x at y 0 Answers
distance between a ray and a point 6 Answers
Find center between 4 points 3 Answers
Trying to cast a ray towards an imaginary wall and get a "hit" / "end" point 2 Answers
finding point on the vector 1 Answer