I Had this silly idea - Edge Collider SnowBoarder Lesson

So, i was doing the edge collider snow ball challenge and, at some point the idea:

“How i make this snowball grow while it’s moving? Like a real snowball” so i came up with this C# script:

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

public class BallGrowth : MonoBehaviour
{
    //Variable Declaration
    [SerializeField] float scalingX = 0f;
    [SerializeField] float scalingY = 0f;
    [SerializeField] float scalingZ = 0f;
    void Update()
    {
        if (GetComponent<Rigidbody2D>().velocity.y > 0)
        {
            transform.localScale += new Vector3(scalingX, scalingY, scalingZ) * Time.deltaTime;
        }

    }
}

BUT the ball still grow even if the velocity is 0, what i’ve done wrong?

It’s not a silly idea. And good on you for giving it a go.

How do you know the velocity is 0? I tested this and for me it was never really working (but I don’t know how you set things up). For me, the y-velocity was never > 0 because my ‘snowball’ was rolling down the hill, so its y-velocity was always negative.
The velocity never actually reached 0 and this is because, as the ball was growing, its center point is moving away from the ground. So, it’s still moving. Or at least this is what I think is happening. If I set the scaling values to 0, the rigidbody eventually goes to sleep (stops processing physics because nothing is really happening anymore), but with the scaling values set (I set all mine to 0.1) it never goes to sleep, and the velocity never stops.
So, how can you fix this? Well, you can stop growing when the velocity gets close to 0. This will then also let the rigidbody eventually go to sleep - which is a good thing.
Change your check (I used the absolute value so it doesn’t matter if it’s negative or positive)

if (Mathf.Abs(GetComponent<Rigidbody2D>().velocity.y) > 0.01f) // when the velocity gets _really_ low, we'll stop growing
{
    transform.localScale += new Vector3(scalingX, scalingY, scalingZ) * Time.deltaTime;
}

Ok, i tried your solution and it works. When the snowball goes into the pit it stop growing.

BUT:

private void Start()
    {
        // Take RigidBody2D Component from GO
        rigidBody = GetComponent<Rigidbody2D>();
    }
    void Update()
    {
        // Take Velocity from rigidBody
        Vector3 velocity = rigidBody.velocity;

        // Calculate velocityOverTime
        float velocityOverTime = velocity.magnitude;

        Debug.Log("Velocity: " + velocityOverTime);

        if (Mathf.Abs(GetComponent<Rigidbody2D>().velocity.y) > 0.01f) 
{
            transform.localScale += new Vector3(scalingX, scalingY, scalingZ) * Time.deltaTime;
        }

    }
}

When i try to check the velocity of the snow ball, and it stop the console says the velocity is 0. That’s the reason why i used “> 0”;

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

Privacy & Terms