1
\$\begingroup\$

I am building simple platformer game in which the player hops on to platforms and the speed of platforms increase per time. This is an endless game and depends on the high score system.

But, the problem is whenever I change the speed of the platform it changes per frame and it increases like 0.1f, 0.2f, 0.3f, and so on. For eg:- my default speed of platforms is 0.1f and whenever the player collides with that platform, I want it to change to 0.2f but since, I did this in update function in unity, it changes 0.1f ,0.2f,0.3f constantly.

My code is:-

public class destroyTime : MonoBehaviour {
    public float startTime = 3f;
public MoveTheTiles movethetiles;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        foreach (Transform child in transform) {
            if (child.tag == "Player" ) {
                startTime -= Time.deltaTime;
movethetiles.move += 0.1f;`//Problem is here!`
                if (startTime <= 0) {
                    startTime = 3f;
                    child.transform.parent = null;
                    Destroy (gameObject);
                }
            }
        }
    }
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ What code do you use to parent the character to the platform in the first place? This is likely to be a better place to trigger the speed change, rather than having every platform search all its children every frame. \$\endgroup\$
    – DMGregory
    Commented May 19, 2019 at 15:27
  • \$\begingroup\$ Also check to limit the velocity using Mathf.Clamp(currentvalue,min,max) or other means to keep between a min and max speed. very important to clamp values in these cases. \$\endgroup\$ Commented May 19, 2019 at 17:41

2 Answers 2

1
\$\begingroup\$

If I understand correctly, it seems that everytime you hit a platform you want the speed to increase by a fixed amount. In this case I would recommend using OnCollision Enter:

void OnCollisionEnter (Collision collision) {
    movethetiles.move+=0.1f
}
\$\endgroup\$
0
\$\begingroup\$

Essentially, you should do something like this...

  1. Give every platform a unique ID of some kind
  2. Give the player a property called something like "current_platform_id"
  3. If player is on no platform, ID is null (or some other value that can't be a platform ID)
  4. As soon as a player touches a platform...
    1. If player.current_platform_id is not null, do nothing
    2. Otherwise, set player.current_platform_id to ID of touched platform and increment platform speed by 0.1f.
    3. As soon as player jumps or moves off the platform, set player.current_platform_id to null.

Effectively, this lets the player object know it's already on a platform, and that it doesn't have to up the speed.

Another approach is for the platform to have a "player is touching" flag, and on player touch and touching==false, set this to true and increase platform speed. As soon as the player leaves the platform (on player jump for example), set platform.touching to false.

This is actually not unlike debouncing a switch in code. This post might give some insights, though switch debouncing often can utilize time checks to avoid jitter (fast on and off fluctuating) at the point of switch activation. https://stackoverflow.com/questions/48434575/switch-debouncing-logic-in-c

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .