- Home /
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:
what should i do, to be able to use the box collider with a custom "gravity" script or why is mine not working?
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.
Answer by PrimeDerektive · Feb 21, 2011 at 09:33 PM
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).
way to ninja xD you mention exactly the same as I did.. only a little better >.>
Answer by Joshua · Feb 21, 2011 at 09:34 PM
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")
Answer by drGsus · Feb 21, 2011 at 10:00 PM
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?
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).
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.
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 :)
$$anonymous$$y thanks goes to you all. Thanks guys for the participation
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.
Answer by Joshua · Feb 21, 2011 at 11:04 PM
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.
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
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.
Your answer
Follow this Question
Related Questions
Check if Rigidbody collider is grounded 3 Answers
Collision not working 1 Answer
Colliders attached to bones 0 Answers
Gravity and physics problem 1 Answer
A Rigidbody moving in a moving Rigidbody 0 Answers