x


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.

more ▼

asked Feb 21 '11 at 09:23 PM

drGsus gravatar image

drGsus
11 7 7 10

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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).

more ▼

answered Feb 21 '11 at 09:33 PM

PrimeDerektive gravatar image

PrimeDerektive
3.1k 57 64 84

way to ninja xD you mention exactly the same as I did.. only a little better >.>

Feb 21 '11 at 09:35 PM Joshua
(comments are locked)
10|3000 characters needed characters left

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")

more ▼

answered Feb 21 '11 at 09:34 PM

Joshua gravatar image

Joshua
6.4k 19 25 70

(comments are locked)
10|3000 characters needed characters left

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?

more ▼

answered Feb 21 '11 at 10:00 PM

drGsus gravatar image

drGsus
11 7 7 10

Use OnCollisionEnter(). OnTriggerEnter() only works if you check the "Is Trigger" field on your collider, and that will make that object able to be passed through (triggers aren't meant for geometry, more for detecting if something is in an area).

Feb 21 '11 at 11:42 PM PrimeDerektive

I tried now: function OnCollisionEnter(hit : Collision){ print ("HIT");}

But as soon as i save the script i get an error:

Script error: OnCollisionEnter This message parameter has to be of type: Collision The message will be ignored.

Feb 22 '11 at 01:19 AM drGsus

Alright problem solved. The reason for the script error was, that I used the name Collision.js. I dont know why, but when I change the script name to playerCollision, everything works fine :)

My thanks goes to you all. Thanks guys for the participation

Feb 22 '11 at 11:02 AM drGsus

No problem :) Yeah, certain words are reserved because they correspond to existing components, Collision is a built-in structure that Unity uses. The same kind of thing would happen if you tried to make a "Transform.js", for example.

Feb 22 '11 at 01:37 PM PrimeDerektive
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Feb 21 '11 at 11:04 PM

Joshua gravatar image

Joshua
6.4k 19 25 70

constant velocity? I can't find such a component neither in physics, nor anywhere else.

the object is just a simple cube, the collision is working, because when it hits an obstacle it stops, but the script just isn't working. Its really frustrating.

I really appreciate your help

Feb 21 '11 at 11:12 PM drGsus

Hmm I think for velocity you'll have to write it into a script that you attach to the object. http://unity3d.com/support/documentation/ScriptReference/Rigidbody-velocity.html here is an example of the code.

Feb 22 '11 at 10:52 AM Joshua
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1783
x1683
x459
x342
x161

asked: Feb 21 '11 at 09:23 PM

Seen: 2357 times

Last Updated: Feb 21 '11 at 09:23 PM