Prevent score when jump and landing is on the same platform

I have an endless jumper where a new platform gets instantiated after reaching the next platform by entering it’s box collider as trigger. Also when you enter the platform’s box collider the score gets inscreased by one.

But here is the problem, if the force on the jump is to low you still land on the same platform so the score gets increased although you are still on the same platform. How can I prevent this from happening without changing the settings on the jumpforce? I was thinking on checking if I’m still on the same platform, but since I’m new to Unity I don’t know really where to start.

(see attached image)

Can someone help me in the right direction?

bce25148d707586020ead7f3f841006874344c58.jpeg

What are you using right now to detect if you landed on a platform?

You can put a box collider in trigger mode on each landing pad. When your character land on the plateform and touch the trigger, you increase the score then disable the collider. So even if you jump back on it, the collider won’t be there anymore so it won’t call the score increase.

Why not just save the last platform jumped on?

If(platformLandedOn != lastPlatformLandedOn)…

1 Like

I thought about that too, but I wasn’t sure if he kept track of the platform. But I think He would have to use the instance ID of the object?

1 Like

I believe if you just assign the gameobject to a variable you can just compare gameobject to gameobject (which may check the id anyways).

However it would also depend. Is the goal only to avoid the last platform landed on or any platform landed on previously, thus only awarding points once for a platform.

1 Like

You might be true I always used instanceID because I presume it would consum less ressources, but I might be wrong.

1 Like

The collider variable with store the instance id as well.

Assuming the collider on the player handles the score increase you could just do this;

Collider lastPlatformJumpedOn;

    private void OnTriggerEnter(Collider other)
    {
        if (score > 0) //For when no platforms have been jumped on so we don't get a null exception
        {
            if (other != lastPlatformJumpedOn)
            {
                lastPlatformJumpedOn = other;
                score += 10;
            }
        }
        else
        {
            score += 10;
            lastPlatformJumpedOn = other;
        }
    }
1 Like

Nope. When you do this:

private GameObject foo;
void OnTriggerEnter(Collider c) {
if (whatever) foo = c.gameObjectt;
}

“foo” doesn’t contain the entire GameObject within itself. It’s a pointer to the actual GameObject in the scene, and in terms of memory it takes up basically the least amount of space that it’s possible for a variable to take up - IIRC exactly as much space as the int that you get from GetInstanceID, and a hell of a lot more useful. Pretty much the only time you’d ever prefer the instance ID is when you’re serializing, and even then it’s rarely useful.

1 Like

Sorry for the late reply, different time zone.
But I got it almost working now except for the first start platform. That platform also gets instantiated.

The code now is as followed. The code is part of the character:

// Increment score and instantiate platform. Also check if last platform was allready landed on
               
 if (ScoreManager.instance != null && GameManager.instance != null) {

                    if (ScoreManager.instance.IncrementScore () > 0) {  //<------ This part gives an error

                        if (target != lastPlatformJumpedOn) {


                            lastPlatformJumpedOn = target;
                            ScoreManager.instance.IncrementScore ();
                            ScoreManager.instance.HighScore ();
                            GameManager.instance.CreateNewPlatformAndLerp (target.transform.position.x);
                        }
                    } else {
                        ScoreManager.instance.IncrementScore ();
                        GameManager.instance.CreateNewPlatformAndLerp (target.transform.position.x);
                        lastPlatformJumpedOn = target;
                    }

             
                }

            }
        }

The problem with this is that I don’t know how to exactly compare the ScoreManager.instance.IncrementScore() with 0 in code. Still very new to coding.

But everybody allready thanks for helping me out.

I would guess that incrementscore is simply a function for increasing the score and doesn’t return anything.

You must have a variable which contains the score, you would compare against that.

1 Like

This is how IncrementScore () looks like and also HighScore (): I don’t know exactly how to compare ScoreManager.instance.IncrementScore() against the score? Sorry still new.

public void IncrementScore () {
      
        score++;
        scoretext.text = "" + score;
    }

    public void HighScore ()
    {



        if (score > highScore) {
            highScore = score;
            PlayerPrefs.SetInt (highScoreKey, highScore);
            PlayerPrefs.Save ();

        }

        highScorePreText.text = "New High";
        highScoreText.text = "" + highScore;


    }

You don’t compare a function against an int. You just need to check if score is > 0.