Changing the "Use Gravity" variable in Javascript

Been trying to find this and failing miserably.

I have a maze set up so that when my character passes under a block, the block is supposed to fall down and block the path so it can no longer be moved.

I’ve encountered two problems with this.

Firstly, and most importantly, even though I’m affecting the blocks with the raycaster I’m using, I can’t change the “Use Gravity” variable from False to True in the Ridged Body… I think I’m using the wrong bit of code and I haven’t been able to find anything to help me.

Secondly, I need a way to make the blocks STOP moving completely once on the ground. I occasionally have a problem where if they character runs into the block hard enough, they’ll send it flying. This just won’t do, since the whole point is that as they character runs through the maze, the maze will shut off paths behind them. Here’s the code I’ve used for the raycaster so far.

function Update () {

var hit : RaycastHit;

if (Physics.Raycast (transform.position, new Vector3(0,1,0), hit, 200) ){
	if (hit.collider.gameObject.tag=="Gate") {
		//turn gravity on for object
	                                                hit.collider.gameObject.GetComponent("RigidBody").UseGravity = true;
		                                           //hit.collider.gameObject.transform.position.y = -1000;
		}
	if (hit.collider.gameObject.tag=="FirstGate") {
		//turn gravity on for object
		hit.collider.gameObject.GetComponent("RigidBody").UseGravity = true;
		//begin counting score
		}
	if (hit.collider.gameObject.tag=="LastGate"){
		//turn gravity on for object
		hit.collider.gameObject.GetComponent("RigidBody").UseGravity = true;
		//place current score as previous score in HUD
		}
	}
}

(notice I have the testing data still in there, commented out, that’s how I know the raycaster is working at all :stuck_out_tongue: it makes the blocks disappear when it hits them, moving them to -1000 on the up-down axis)

The stuff that’s been commented out isn’t relevant just yet, I’ll MAYBE need help on those later, but I want to try and figure it out on my own first since that’s kinda the point of this project. Only reason I’m here is that I’ve exhausted other options ^^;

For your first issue try:

whateverGameObject.rigidbody.useGravity = true;

As for the second issue:

Try adjusting the mass of the object in the inspector

PERFECT! Thank you. Finally got that bit working. Still having SOME issues with the blocks being pushed around…

hmmm… I wonder… is there a way to make them slowly fall down until they’re just A BIT through the floor? and then freeze right there?

edit: also, is there a way to measure velocity? as in, if the position of an object is changing, not just it’s current speed (so that it’ll register running into a wall and not moving as the same as if no one were hitting the forward button)

Something like this

velocity.y += Physics.gravity.y * Time.deltaTime;

well that would work for the blocks, but I need it to measure the velocity of the character… could I make multiple forms of that then have another line add them together for the true velocity of the character?

You can make a rigidbody unresponsive to collisions by setting its isKinematic property to true. If you do this in the object’s OnCollisionEnter function, it should stop where it lands. There is a chance it might get frozen in a slightly awkward position rather than flat on the ground - you will need to test this in your game and maybe look for another solution if it’s a problem.