Working on boost, slow and normal speed

When working on the speed of the driver, I want the driver to speed up when it picks up the speed up token (as described in the video) and slow down when it collides with an object (as described in the video). I got those methods to work, but I also want the driver to return to normal speed when it touches the street again.
Variables

I have created some bool variables to act as conditions for when things happen.

Example:
(Line 23) When the driver hits an object with the “Bumps” tag, they will slow down. This makes it so that the driver slows down and the Boolean conditions change to fit the situation.

(Line 32) When the driver interacts with the street and is slowed down, make it so it returns to normal speed.

I have yet to make the normal speed function to work properly and would like to know what approach should I go for in this scenario.

1 Like

Hi,

It’s great to see that you are challenging yourself. :slight_smile:

Your code looks fine to me. The question is whether the if-blocks get executed as expected.

Do you need so many bool variables to make your idea work? If not, try to use as few as possible because they add a lot of complexity to your algorithm. The more complex an approach is, the more challenging it is to find a solution.

In your code, you already have the solution for your problem: You assign a value to moveSpeed in specific cases. Is slowSpeed the “normal speed”?

I wanted to accomplish the same thing. This can be done with an additional variable and the OnTriggerExit2D() method.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Driver : MonoBehaviour
{
    [SerializeField] float steerSpeed = 1f;
    [SerializeField] float moveSpeed = 10f;
    [SerializeField] float slowSpeed = 3f;
    [SerializeField] float boostSpeed = 20f;
    public float oldSpeed;

    void Start()
    {
        oldSpeed = moveSpeed;
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Boost")
        {
            Debug.Log("Boost!");
            moveSpeed = boostSpeed;   
        }
        if (other.tag == "Bump")
        {
            Debug.Log("Bump!");
            oldSpeed = moveSpeed;
            moveSpeed = slowSpeed;      
        }
    }

    void OnTriggerExit2D(Collider2D Bump)
    {
            Debug.Log("Done bumping!");
            moveSpeed = oldSpeed;      
    }

    void OnCollisionEnter2D(Collision2D Bump)
    {
        Debug.Log("Bump!");
        moveSpeed = slowSpeed;
    }
    
    void Update()
    {
        float steerAmount = Input.GetAxis("Horizontal") * steerSpeed * Time.deltaTime;
        float moveAmount = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
        transform.Rotate(0, 0, -steerAmount);
        transform.Translate(0, moveAmount, 0);
    }
}

As you can see, I instantiated an arbitrary integer variable called oldSpeed. This was so that I had somewhere to store the speed before entering the trigger changes it. This is demonstrated by the exchange of variables below. Say moveSpeed equals 20f. We store that into the variable oldSpeed. Now, that value is safe, so we can change the moveSpeed variable. Say, moveSpeed = 10f.

     oldSpeed = moveSpeed;
     moveSpeed = slowSpeed;    

So how do we access the default speed? By using oldSpeed! Since it equals 20f.

To return to the default speed provided by the player (in this case, what was provided in the Serialized Field), we need to use an OnTriggerExit2D() function, like so.

    void OnTriggerExit2D(Collider2D Bump)
    {
            Debug.Log("Done bumping!");
            moveSpeed = oldSpeed;      
    }

Thus, upon entering the trigger, the speed is saved as a variable. The trigger changes the speed of the car. Upon exiting the trigger, the speed that was saved to the variable is returned to the car, allowing it to return to the default speed.

… Sorry for saying “speed” a lot, I hope this helped! :grin:

Good job, and thanks for sharing your solution! :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms