Transform.position with Box Collider?

Hey guys,

I have a problem here.

I'm using a script wich kinda simulates gravity:

var fallSpeed = 1;

function Update(){

transform.position.y -= fallSpeed;

}

So far so good. My object is falling. Now I want to check, if the object collides with something on the way. I`m using:

function OnControllerColliderHit(hit : ControllerColliderHit) { if (hit.gameObject.tag == "obstacle") { print ("Collision"); } }

But it isn't working. I have two questions:

  1. what should i do, to be able to use the box collider with a custom "gravity" script or why is mine not working?

  2. is there another possible way to simulate gravity, which isn't accelerating over time, but keeps a steady pace? a rigidbody would solve my collider problem, but it accelerates over time.

OnControllerColliderHit() only works with CharacterControllers, not colliders. The only way to detect a collision with a collider is by using OnCollisionEnter() or OnTriggerEnter(), and one of the two objects colliding needs to have a rigidbody component.

And even if you were using a CharacterController, OnControllerColliderHit() is only called during a collision when the CharacterController object is being moved with a CharacterController move function (.Move or .SimpleMove). You are moving your object by directly modifying transform.position, so it wouldn't work.

Your best bet would be to give your object with the box collider a rigidbody component, disabling the rigidbody gravity (its just a checkbox in the inspector), then handling your own gravity just like you are (but you probably want to multiply fallspeed by Time.deltaTime, otherwise it is framerate dependent, and will fall faster on some computers than others).

Rigidbody would solve the gravity problem by indeed accelerating (which is what gravity does). If you want a constant fallingspeed you could add a constant force though (Component > Physics > Constant Force)

An other thing, right now your transforming your position.y by fallSpeed every frame, meaning that fallspeed depends on the fps of the computer you're using. Try avoiding this by using time.

You might want to instead of using the OnControllerColliderHit function the OnTriggerEnter function. Make the object it's colliding with a trigger (! do not forget that)

function OnTriggerEnter (hit : Collider) { if(hit.gameObject.tag == "tag, in your case Obstacle")

For some reason the comment function isn't working, so I try an answer.

alright.

I attached the rigidbody to my falling object, disabled the gravity. I tried using a constant force, but it still is accelerating. it's slow in the beginning and accelerates over time.

Now my object collides with the obstacles. It stops when it collides contrary to my previous try, where it just rushed through it.

my falling scripts looks now like this:

var fallSpeed = 50;

function Update () {

transform.position.y -= (fallSpeed*Time.deltaTime);

}

the framerate vs. time problem should be solved like that right?

my collision script looks like this:

function OnTriggerEnter (hit : Collider) { if(hit.gameObject.tag == "obstacle") { print ("HIT");

}

}

but no log is written. I double checked the tags, everything should work, but it doesn't.

Any ideas?

For the collision not registering check if the character controller collision mesh is not inside a biggest other one that's preventing it from colliding with the obstacle.

For a constant speed don't use force but use velocity, my mistake.