BASIC TELEPORTING QUEST: 'JumpyJumpy' - Solutions

Feel free to share your solutions, ideas and creations below. If you get stuck, you can find some ideas here for completing this challenge.

2 Likes

Hey Rick ! Thanks for the challenges !
So here is what I have done for this first challenger.

public class Bounce : MonoBehaviour
{
     [SerializeField] float jumpForce = 1000f;

     void OnTriggerEnter(Collider other)
     {
          JumpyJumpy(other);
     }

     void JumpyJumpy(Collider other)
     {
          other.attachedRigidbody.AddForce(0, jumpForce, 0);
     }
}

And really important here… if you don’t want to aim straight for the stars or even the moon… don’t forget to restet your Bounce component… it’s set to something like 7000 so you are going to fly pretty high.

3 Likes

Haha, I still a real kick out of seeing people first implement this and have their player get shot to the moon. :slight_smile:

8 Likes

I didn’t get shot to the moon right away as I did something terrible first… I didn’t use a variable and put something like 500 right there in my method (I know, terrible sin).

But yeah when I changed to use jumpForce instead and I thought I had everything all neat and tidy… I was quite surprised to see my player take flight so dramatically. Serves me right for not paying attention :wink: !

1 Like
public class Bounce : MonoBehaviour
{
    [SerializeField] 
    float jumpForce = 1000f;

    void OnTriggerEnter(Collider other)
    {
        // Challenge 1: JumpyJumpy(other);
        JumpyJumpy(other);
    }

    void JumpyJumpy(Collider other)
    {
        if(other.gameObject.CompareTag("Player"))
        {
            other.gameObject.GetComponent<Jump>().rigidbody.AddForce(Vector3.up * jumpForce);
        }
    }
}

First I’m checking if the player stepped on the bounce pad, and if so I’m adding force using the jumpForce variable.

2 Likes

Here’s my solution:

void JumpyJumpy(Collider other)
{
    var rigidBody = other.gameObject.GetComponent<Rigidbody>();
    if (rigidBody != null)
        rigidBody.AddForce(Vector3.up * jumpForce);
}

I liked @AssaSquid’s attachedRigidbody solution. I didn’t know about that property. But is it safe to not check for null or is that something we should do?

4 Likes

I actually didn’t know about it either. I just did a quick search because my autocompletion offered “attachedRigidbody” as something I could type. And basically it seemed to do the trick so that was nice.
And yes, I think it’s safer to check if rigidBody is not null like you did!

1 Like

Is there any benefit to putting the code into the JumpyJumpy() method and calling that in OnTriggerEnter(), other than to save clutter if there was more happening? :slight_smile:

Is there a way to make the velocity of the player count too? Like, if a player jumps off a platform at a certain height onto a jump pad, he will bounce back up at the same height.

It helps with self documenting (assuming you use a decent method name)

I see a trigger happening and then its doing a jump
Instead of
I see a trigger now it’s geting a component or doing something to the rigidbody etc wodnering whats it’s doing…

It took me a while to figure this out, but I was able to take a peak at the jump script for the player to learn that AddForce() was a thing. Now I know to pay attention to odd capitalization in the quest challenge text for hints.

    void JumpyJumpy(Collider other)
    {
        other.GetComponent<Rigidbody>().AddForce(Vector3.up * jumpForce);
    }

I found that the immediate jump when entering the square was a bit too spontaneous. Made a coroutine which waits for a small delay before pushing the player in the air.

public class Bounce : MonoBehaviour
{
    [SerializeField] float jumpForce = 1000f;
    [SerializeField] float jumpDelay = 0.3f;
    private bool isOnPlatform;

    void OnTriggerEnter(Collider other)
    {
        isOnPlatform = true;
        StartCoroutine(BounceObject(other));
    }

    private IEnumerator BounceObject(Collider other)
    {
        yield return new WaitForSeconds(jumpDelay);
        if (isOnPlatform) other.attachedRigidbody.AddForce(Vector3.up * jumpForce);
    }

    private void OnTriggerExit(Collider other)
    {
        isOnPlatform = false;
    }
}
2 Likes

Hey, this is my solution for the Jumping.

When the Player triggers the JumpPad, a force is added to its rigidbody.

    [SerializeField] float jumpForce;
    private readonly Vector3 _jumpDirection = Vector3.up;
    
    void OnTriggerEnter(Collider other)
    {
        JumpyJumpy(other);
    }

    void JumpyJumpy(Collider other)
    {
        if(other.CompareTag("Player"))
        {
            other.gameObject.GetComponent<Rigidbody>().AddForce(_jumpDirection * jumpForce * Time.deltaTime, ForceMode.Impulse);
            Debug.Log("Jump");   
        }
    }

Hi, just sharing my solution for this:

public class Bounce : MonoBehaviour
{
    [SerializeField] float jumpForce = 1000f;
    
    void OnTriggerEnter(Collider other)
    {
        // Challenge 1: JumpyJumpy(other);
        JumpyJumpy(other);
    }

    void JumpyJumpy(Collider other)
    {
        if (!other.CompareTag("Player")) return;

        Rigidbody rigidbody = other.GetComponent<Rigidbody>();

        if (rigidbody == null) return;

        rigidbody.AddForce(Vector3.up * jumpForce);
    }
}

I wanted to put the CompareTag-Check into the OnTriggerEnter-Method but I guess if there is more logic in the future, the OnTriggerEnter might get cluttered. At the same time (while JumpyJumpy is a descriptive name), it should only contain what it describes. What do you think about it?

I learnt that checking null on objects is better done with ifs / ternary operators instead of Null propagation:

// will show you "UNT0008 C# Unity objects should not use null propagation."
rigidbody?.AddForce(Vector3.up * jumpForce);

// do instead:
if (rigidbody != null) { ... };
1 Like

my solution

{
    [SerializeField] float jumpForce = 1000f;
    [SerializeField] Rigidbody rb;
    
    void OnTriggerEnter(Collider other)
    {
        // Challenge 1: JumpyJumpy(other);
        JumpyJumpy();
    }
    void JumpyJumpy(Collider other)
    {
        rb.AddForce(transform.up * jumpForce);
    }
}

Hi Rick,

So this was my take on the JumpyJumpy challenge

image

1 Like

Hi Rick, this is my code for the JumpyJumpy challenge.

I wanted to stop the character from jumping if the zone is exited, so i added a way to stop the coroutine!

2 Likes

Interesting solution!

1 Like

Here is my solution for the Jump Problem.

public class Bounce : MonoBehaviour
{
    [SerializeField] float jumpForce = 1000f;
    
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            JumpyJumpy(other);
        }
    }

    void JumpyJumpy(Collider other)
    {
        if (other.TryGetComponent(out Rigidbody rb))
        {       
           rb.AddForce(rb.velocity.x, jumpForce ,rb.velocity.z);
        }
    }
}

I could add a coroutine to have a delay and make it smooth.

I gave the Player the “Player” tag and added this code. As previous questers I accidentally did not change the jumpForce in the inspector at first, and subsequently went orbital, haha! But then I changed it to a 1000 which seemed reasonable and now its working well :slight_smile:

public class Bounce : MonoBehaviour
{
    [SerializeField] float jumpForce = 1000f;
    
    void OnTriggerEnter(Collider other)
    {
        JumpyJumpy(other);
    }

    void JumpyJumpy(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            other.GetComponent<Rigidbody>().AddForce(transform.up * jumpForce);
        }
    }
}
1 Like

Privacy & Terms