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.
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.
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.
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;
}
}
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.
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.
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.