Home > Blockchain >  What would the best method be to make the player increase in size when they touch a "food"
What would the best method be to make the player increase in size when they touch a "food"

Time:11-03

I'm very new to Unity and I'm developing a game were the player gets bigger every time they eat a food pellet.

I've already implemented the food mechanic but the player doesn't become bigger yet. What would be the best way of going about this? I know I might have to increase the scale of the Collison sphere and game object (duh) and I will have to increase the hight of the camera since it is a top down perspective that's very close to the player character. I know this I just don't know how.

I've tried directly setting the scale inside the if statement that controls the eating of the food, no luck.

my if statement if that helps:

private void OnTriggerEnter(Collider collision)
    {
        if (collision.tag == "Food Particle")
        {
            growth  = 1;
            Debug.Log(growth);
            collision.gameObject.SetActive(false);

        }
    }

Help would be appreciated!

CodePudding user response:

If you mean the GameObject should get bigger then you could change the scale. Best would be to make an animation which makes the mesh get bigger. But this isn't a tutorial forum. So limited to 'to get something on screen fast':


Please consider to take those dull tutorials. They only seem not to talk about your goal/game. They actually do.


private float scale_min         = 1.0f;
private float scale_max         = 2.0f;
private float energy_max        = 1.0f;
private float energy_current    = 0.0f;

// grow the game object
private void UpdateScale()
{

    Vector3 scale        = transform.localScale;
    scale.y              = scale_min   (scale_max - scale_min) * energy_current;
    transform.localScale = scale;

}

// a method which consumes the food
// return true when consumed, otherwise false
private bool Feeding(float growth)
{

    if(energy_current <= energy_max)
        return false;

    energy_current  = growth;
    if (energy_current > energy_max)
        energy_current = energy_max;

    return true;

}

private void OnTriggerEnter(Collider collision)
{

    if (collision.tag == "Food Particle")
    {

        // if the food is consumed, remove the food game object
        if(Feeding(0.1f))
        {

            collision.gameObject.SetActive(false);

        }

    }

}

Another script at the 'food' GameObject with a value of energy, or growth, would make the mechanism more generic.

public class Food : MonoBehaviour
{
    public float energy = 0.1f;
}

This will need an adjustment for your OnTriggerEnter.

private void OnTriggerEnter(Collider collision)
{

    // Get the Food component from the food game object
    Food food = collision.GetComponent<Food>();

    // Check if the game object actually has one
    if(food)
    {

        // if the food is consumed, remove the food game object
        if(Feeding(food.energy))
        {

            collision.gameObject.SetActive(false);

        }

    }

}

Rather then setting collision.gameObject inactive, destroying the object seems to be more appropiate.

Destroy(collision.gameObject);

CodePudding user response:

you could simply create a parent game object and put everything inside and basically increase the scale of the parent object which will scale every object under it!

  • Related