Cannot implicitly content "bool" to "float"

Deleted

Because your SetRigidBody function is trying to enter a bool, for mass, which is a float value… Try adding an if/then statement to your SetRigidbody function, that on false, sets the mass to 0, and on true, sets it back to its original mass. :slight_smile:

1 Like

Please look at this page for posting code on the forums: Using code tags properly

If you alter the method signature so it takes a float as a parameter, you could send the float, instead :slight_smile:

if you really had (want) to keep a bool, you could do this:

private void SetRigidbody(bool j)
{
if(j == false)
   this.GetComponent<Rigidbody>().mass = 0;
else GetComponent<Rigidbody>().mass = 1; // or whatever it is supposed to be.
}
2 Likes

You can visit this link for help on getting started: Learn
and this: Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn
and this: Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn

First the quick and dirty answer:

private void SetRigidbody(bool j)
{
     if (j == false) {
          this.GetComponent<Rigidbody>().mass = 0.0f;
     } else {
          this.GetComponent<Rigidbody>().mass = <WHATEVER MASS IT'S SET AT INITIALLY>;
     }
}

Or, the proper way to do this, if you’re not going to want to ever set it to any values other than the starting mass and 0.0 mass, would be to add a variable to your class, outside of your function calls, and on your start method, set this to your mass. Then, when you run your SetRigidbody, just set it to that value. This would allow you to change your mass in the editor, without having to change your script.

private float initialMass;

void Start()
{
     initialMass = this.GetComponent<Rigidbody>().mass;
}

private void SetRigidbody(bool j)
{
     if (j == false) {
          this.GetComponent<Rigidbody>().mass = 0.0f;
     } else {
          this.GetComponent<Rigidbody>().mass = initialMass;
     }
}
1 Like

Sorry, I assumed the CODE tag would automatically inset bracket’ed code… just edited for clarity.

1 Like
private void StopSpinning()
{
     this.GetComponent<Rigidbody>().angularVelocity = new Vector3(0.0f, 0.0f, 0.0f);
}

Calling that should remove any sort of rotation being done by the rigidbody.

1 Like

FYI, you can also make it equal Vector3.zero. That’s a shorthand version.

I think you’re trying to pass it in a value. Since StopSpinning doesn’t have anything between the parantheses, you don’t pass it any value

1 Like

No. You’re inexperienced. All of this comes with experience, time and practice.

2 Likes

OH! Okay, so you need to set both the “velocity” AND “angularVelocity” to Vector3.zero in order to prevent that from happening. :slight_smile:

Maybe just make it kinematic when you pick it up.

Yes, or set the velocity to zero, too.

I’m not sure you’d want a dynamic rb as you walk around though, but who knows.

In that case, you’d want to follow methos5k’s suggestion.

When you pick it up

GetComponent<Rigidbody>().isKinematic = true;

When you drop it

GetComponent<Rigidbody>().isKinematic = false;

This will make it immune to physics while you’re holding it.

Pro-Tip:
In they object’s start function, create a reference to rigidbody, so you don’t have to run GetComponent() all the time. It will make it quicker. :slight_smile: I’m not gonna tell you how to do that one though. :stuck_out_tongue: Practice! :slight_smile:

Glad you got it working. :slight_smile:

1 Like