Falling object when player approach

Guys any help for a falling object when player approach to the scene

Example:

  • player walking to a house when
  • suddenly a ceiling falls down to the ground.

help how to code it i have no idea how to do it. im just new in unity.

There are different way to approach this. The easiest way it probably by setting up a collider in game, when the player walks through the collider the ceilling will fall down.

    void  OnTriggerEnter(Collider other)
    {
    if(other.tag == "Player"){
    GameObject.FindWithTag("Ceilling").GetComponent<Rigidbody>().isKinematic = false;
    }

This script should be attached to the desired collider. This collider will then act as a barrier, when you walk into this barrier the code will execute the lines within the brackets. For this instance it will look if the other collider (The player) has the tag “Player” attached to it. If it does the code within that if-statement will be executed.

The code within the if-statements tells Unity to find an object with the tag “Ceilling” and will then set the isKinematic to false

(isKinematic is part of the rigidbody component)

-Things to be aware of.

  • both the objects should have a collider.(The player and the desired collider) which one of them has the isTrigger set to true.
  • The Ceilling should have a rigidbody that is set to isKinematic = true.
  • Both objects has their respective tags.

Hope this helped you a bit. If you find coding difficult, I would take a look at tutorials from Unity or on Youtube.

//LINK to Unity scripting tutorials. (I would learn C# because this is needed if you ever want to make mobile games in the future. You can’t make mobile games with UnityScript because it is a dynamic scripting method.)

Try this if we hit a collider the component gravityScale changes to 1 and the object starts falling

// Start is called before the first frame update
void Start()
{

    GetComponent<Rigidbody2D>().gravityScale = 0;

}

// Update is called once per frame
void Update()
{

}

void OnTriggerEnter2D(Collider2D coll)
{
    
    string tagName = coll.gameObject.tag;
    
        if (tagName == "love")
        {
            GetComponent<Rigidbody2D>().gravityScale = 1f;

        }
            

}

,Try this.

// Start is called before the first frame update
void Start()
{

    GetComponent<Rigidbody2D>().gravityScale = 0;

}

// Update is called once per frame
void Update()
{

}

void OnTriggerEnter2D(Collider2D coll)
{
    
    string tagName = coll.gameObject.tag;
    
        if (tagName == "love")
        {
            GetComponent<Rigidbody2D>().gravityScale = 1f;

        }
           

}