Jahmez
Jahmez

Reputation: 13

Awaken Rigidbody in Unity

Ok - I'm super new at Unity (just learning for fun) and wanted to have an enemy cube fall as the player gets within 15 on the Z. I can get the rigidbody function of the enemy cube to 'sleep' but then when I get within 15 or less, it will not awaken and start to fall. Can you help me with my code? The Debug.Log is telling me what I want as I run it, but the rigidbody is not reactivating on the enemy cube. Sorry if this is a super simple request...just trying to learn!

     using UnityEngine;

public class activatefall : MonoBehaviour
{
    public Transform Player;
    public Rigidbody rbgo;
    private float coolnumber;
    private float badtogood;

    // Update is called once per frame
    void FixedUpdate()
    {
        coolnumber = transform.position.z;
        badtogood = coolnumber - Player.position.z;
        Debug.Log(badtogood);

        if (badtogood < 15f)
        {
            rbgo.WakeUp();
            Debug.Log("Falling!");
        }
        else
        {
            rbgo.Sleep();
            Debug.Log("Frozen");
        }

    }
}

Upvotes: 1

Views: 364

Answers (1)

Daniel
Daniel

Reputation: 7724

If you want a Rigidbody to be stopped and then fall, you can just use rbgo.useGravity = false/true.

There are other ways though, you can play with RigidbodyConstraints, making the Rigidbody freeze in Y axis and then remove this constraint.

If you want to stop a Rigidbody completely after it moves, you can just do rbgo.constraints = RigidbodyConstraints.FreezeAll or rbgo.velocity = Vector3.zero (and then, if you want to disable the gravity, you do rbgo.useGravity = false.

You can also use transform.position and/or transform.Translate if you don't want to deal with the Rigidbody itself.

Upvotes: 1

Related Questions