0
\$\begingroup\$

I'm trying to create a mechanic in my puzzle game in which if the player clicks on one of the flying game objects, the object's gravity would be enabled and world physics would be applied to it. Initially, I thought I could use the 'enabled' feature to create such a mechanic but I noticed that the RigidBody class does not contain 'enabled'.

Could there be any other way that would allow me to disable the GameObject's gravity or RigidBody component when it is instantiated?

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

[RequireComponent(typeof(Rigidbody))]
public class Plane : MonoBehaviour
{
    private bool clicked = false;
    [SerializeField] private Rigidbody rb;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.enabled = false;
    }

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

    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Presumably you thought about toggling the body's useGravity property, zeroing its velocity, or setting its constraints to block unwanted motion? \$\endgroup\$
    – DMGregory
    Sep 22, 2019 at 16:57
  • \$\begingroup\$ @DMGregoryThe effect I wanted to create is something like This: A box that is in a "frozen state" and it is in the air. Once the player clicks on that box, it would get out of its frozen state and gravity would start to effect the object. \$\endgroup\$ Sep 23, 2019 at 3:26

2 Answers 2

0
\$\begingroup\$

You could do this by enabling and disabling the "isKinematic" property of the rigid body: For more information about "isKinematic" is here:

rb.isKinematic = false;

or to enable it do it like this:

rb.isKinematic = true;

here is your script:

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

[RequireComponent(typeof(Rigidbody))]
public class Plane : MonoBehaviour
{
    private bool clicked = false;
    [SerializeField] private Rigidbody rb;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.isKinematic = false;
    }

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

    }
}

I hope this will help.

\$\endgroup\$
3
  • \$\begingroup\$ This allowed my game objects to be frozen in the air, thank you. But I forgot to mention that my game objects cannot be affected by any collision motion (So it will remain still after any collisions with other objects) \$\endgroup\$ Sep 23, 2019 at 3:40
  • 1
    \$\begingroup\$ The kinematic setting should accomplish that effect as well. \$\endgroup\$
    – DMGregory
    Sep 23, 2019 at 11:38
  • \$\begingroup\$ Other objects collision will not effect Kinematic Object. And i am glad that i was helpful. \$\endgroup\$
    – Swati
    Sep 23, 2019 at 12:04
0
\$\begingroup\$

Why you are not simply using useGravity to control the gravity of the GameObject?

rb.useGravity = false;

or

rb.useGravity = true;

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

[RequireComponent(typeof(Rigidbody))]
public class Plane : MonoBehaviour
{
    private bool clicked = false;
    [SerializeField] private Rigidbody rb;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.useGravity = false;
    }

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

    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .