Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by drGsus · Feb 21, 2011 at 09:23 PM · rigidbodycollidergravitybox

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joshua · Feb 21, 2011 at 09:35 PM 1
Share

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

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PrimeDerektive · Feb 21, 2011 at 11:42 PM 0
Share

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

avatar image drGsus · Feb 22, 2011 at 01:19 AM 0
Share

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.

avatar image drGsus · Feb 22, 2011 at 11:02 AM 0
Share

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

avatar image PrimeDerektive · Feb 22, 2011 at 01:37 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image drGsus · Feb 21, 2011 at 11:12 PM 0
Share

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

avatar image Joshua · Feb 22, 2011 at 10:52 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges