- Home /
Other
Can you determine the speed of falling?
Could anyone help change my script?
I have an object "Ball".
My object is a ball and it bounces up.
Every time when I press the left mouse button, "Ball" jumps up.
Each up move adds points.
Now my question.
When the object falls, the points decrease, but the speed of free fall continues to grow.
This causes points to not keep up with the falling ball.
The result is that when the ball stop to the ground, instead of having 0 points it has, for example, 66.
Can you determine the speed of falling?
How do that?
For now, I just add to my object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour {
public Text scoreText;
[SerializeField]
int score = 0;
[SerializeField]
Vector3 _lastPosition;
// Use this for initialization
void Start () {
_lastPosition = this.transform.position;
}
// Update is called once per frame
void Update () {
if( this.transform.position.y > _lastPosition.y )
score++;
else{
score--;
}
_lastPosition = this.transform.position;
scoreText.text = "Score" + score;
}
}
Answer by anthot4 · May 09, 2018 at 02:24 PM
You could check the velocity of the rigidbody as soon as the ball jumps. Have two if statements one to check for positive velocity and if it is add to the score and if it is negative subtract from the score. Have this inside your if statement which check for left mouse button down on line 20.
If I can be honest, I will know how to do it with the condition of adding points, but with subtraction I will not know how to do it.
Something like this:
Vector3 LastPosition = Vector3.zero;
Void FixedUpdate(){
speed = (transform.position - LastPosition).magnitute; LastPosition = transform.position;
if (Input.Get$$anonymous$$ouseButtonDown(0){
if (speed >=0){ Score++; }
if (speed < 0){ Score--; } }
}
Answer by polan31 · May 09, 2018 at 02:48 PM
Can not somehow be addicted to the height Y?
For example, as the height grows and the points grow, as the height fall and the points fall.
You could do that it would be something like this:
score += transform.y; // this would add the transform point on y to your score.
If your doing it that way it would be best to put the code above in a coroutine then add it to the players score every second or so.
Ok, I found something like this on unity servers and works in my 2d application.
https://answers.unity.com/questions/613277/how-to-increase-score-with-y-axis.html
However, I still can not transform it in such a way that the points are going down.
Answer by Guy_Yome · May 10, 2018 at 07:55 PM
Okay I have a solution. I assume that your ball will be jumping on platforms that are maybe generated on the "fly". So you'll have to make a list of platforms where you will add all the new platforms you create at the end of the list. You could use the collision detection built in Unity to help you detect what you are doing. Whenever you land on a new and higher platform, you will have to remove the first element in the list (which is the platform below). Doing so, the only platforms in the list will be the platform you are on and the platforms that are above you (Shown in blue).
Whenever you fall and collide with a platform that is not in the list (Shown in red), it will mean that it was a platform below and you will make the player lose points. Whenever you land on the first element of the list, you landed on your own platform and gained nothing. And for the rest (above), you will gain points. One other case is if you fall in the void (not on a platform), you could use a height that is an arbitrary distance lower than the actual platform (red line in the drawing) to see if the ball is lower than that. If it is, lose points and respawn the ball on the last platform. I hope this helps.
The blue platforms shown are the ones still in the ist. The red ones are not in the list anymore. The arrows say what actions you did to reach this position. down = fall, up = jump The red line is the point of falling in the void. The green circle is your ball.
Follow this Question
Related Questions
Adding and Substracting ammunition 1 Answer
weird result when substracting/adding; 1 Answer
JavaScript: Sound & Points deduct On click Help! 0 Answers
Problem with reducing points 1 Answer
Trigger animation OnMouseDown 0 Answers