Consider this Unity C# script snippet attached to a GameObject with a Rigidbody component:
void Start() {
Rigidbody rb = GetComponent();
rb.velocity = new Vector3(0, 10, 0);
Debug.Log(rb.velocity.y);
} What will be printed in the console when the game starts?
void Start() {
Rigidbody rb = GetComponent<Rigidbody>();
rb.velocity = new Vector3(0, 10, 0);
Debug.Log(rb.velocity.y);
}Remember that rb.velocity is a Vector3 representing speed in each direction.
The velocity is set to (0, 10, 0), so rb.velocity.y is 10, which is printed.
In Unity's Rigidbody 3D component, which property determines whether the object is affected by gravity?
Think about the property that toggles gravity on or off.
The useGravity property enables or disables gravity's effect on the Rigidbody.
Look at this code snippet:
void Start() {
Rigidbody rb = GetComponent();
rb.isKinematic = true;
rb.AddForce(new Vector3(0, 500, 0));
} Why does the Rigidbody not move when the game runs?
Check what isKinematic does to Rigidbody behavior.
When isKinematic is true, the Rigidbody ignores physics forces like AddForce.
Choose the correct C# code snippet to apply an impulse force upwards to a Rigidbody named rb:
Impulse force applies an instant push. Check the ForceMode enum.
AddForce with ForceMode.Impulse applies an instant force. Option A uses default ForceMode.Force which is continuous. Option A is invalid method.
Given a GameObject with two Rigidbody components attached (which is unusual but possible), and this code runs:
void Start() {
Rigidbody[] rbs = GetComponents();
foreach (var rb in rbs) {
rb.isKinematic = true;
}
rbs[0].isKinematic = false;
} How many Rigidbody components will be affected by physics (not kinematic) after Start?
Think about how the loop sets all to kinematic, then one is set back.
Initially both are set to kinematic (no physics). Then the first Rigidbody is set to non-kinematic, so only one Rigidbody is affected by physics.