- Home /
How to "Increase score with Y axis"?
Hi, i would like to know how can i Jscript this: If the player goes up following the y axis, the score increases. If the player falls down, or goes down following the y axis, the score does not decrease.
This may seem obvious to you, but not to me unluckily :( Everything i looked for on the internet seemed unadapt for me and so it looks like my only chance is asking here ^^
Thanks for your help :3
Answer by Mikea15 · Jan 10, 2014 at 12:39 PM
You could go for something like this.
if your player's position is higher than your players last position increment score. or if the difference from your previous height and your current height is greater than 0, that means you've gone up. if its lesser than 0, you're falling down. also, you'd have to save your last position in the end.
using UnityEngine;
using System.Collections;
public class JumpIncrementScore : MonoBehaviour
{
[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 ) > 0 )
score++;
_lastPosition = this.transform.position;
}
void OnGUI( ) {
GUILayout.Label("Score : " + score);
}
}
It gives me an error i can't eli$$anonymous$$ate on: Vector3 _lastPosition; telling me that a namespace can't contain things like methods, or something similar (had to translate from italian). Any idea?
I just updated the code. I was forgoting to update the position. Are you using Javascript? This is c# code, and obviously you'll have to put this inside a Class. I haven't tested this, but will do. Let me know if you can't get there
Hey, Headstone7, I've just updated the code above and tested it and it works fine. When you go up, it will increase the variable score. Just drag this c# script into a gameobject, hit play, and raise the object on the Y coordinate and you'll see the score incrementing. ;)
Cheers
Yes, Player would be a class where you would expose a static Score variable. Assu$$anonymous$$g you'd have that. :) Just use the updated code above. ;) it should work fine.
That's fantastic man! Thank you! Unluckily this also increases score with x modifications for some reason, i'll try to figure out how to remove this issue, let me know if you can imagine some solution ^^ Thanks again!