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 as42 · Jul 10, 2014 at 03:59 AM · 2dcollisionphysicsrigidbodykinematic

2D motion/collision: physics, or direct translation?

Found out the hard way today that if you simply translate an object by calling its position.transform and setting it to some other vector2 (which I've been doing to move my character), collisions don't trigger. To actually make them trigger, you need to do it in the physics. So instead of directly altering the character's x,y position (this is a pixel-based flat 2d game, by the way, like original Zelda) you need to alter its velocity, which isn't as pretty.

But even worse than having to get your hands dirty with velocity is how, to make physics collisions work, your rigidbody can't be kinematic. Meaning gravity will effect it. Of course, there are ways around this, but by this point I feel like I'm going very very out of my way to make something as simple as a collider work (this is an old-school flat 2d game, anyway -- I don't even need true physics at all).

I'm wondering what is the best way to do this?

I have a character sprite walking along the floor. I don't want him to be able to walk through the wall. What's the simplest way to do this?

Thanks.

EDIT: I just saw these two hints, but neither of them work for me:

  • If you are directly manipulating the Transform component of your object but still want physics, attach a Rigidbody and make it Kinematic.

  • If you are moving a GameObject through its Transform component but you want to receive Collision/Trigger messages, you must attach a Rigidbody to the object that is moving.

If I make my character kinematic and then alter its Transform.position to relocate it pixel by pixel, eventually hitting a collider, nothing happens: the object goes right through it, and the collider method isn't even called.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Noob_Vulcan · Jul 10, 2014 at 04:24 AM

Collision Detection is done by Physics Engine. So to detect collision

a.You should have a Rigidbody component attached to the game object that has the script containing the OnCollisionXXXX, OnTriggerXXXX methods on it.

b. U can set gravity to false of any rigidbody.

In Your case ... Make Character Rigidbody with gravity on/off as you wish. Dont Set isKinematic=True. And Move your character by adding force to it.

Or You can use Character Controller if you dont want rigidbody i.e Physics .

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 as42 · Jul 10, 2014 at 05:30 AM 0
Share

Thanks. What I ended up doing was setting gravity to 0, and turning is$$anonymous$$enematic=false. I don't move the character by adding force, though -- turns out in this case you can use transform.translate (and maybe also even transform.position) and collisions will trigger as long as kinematic=false.

avatar image
1

Answer by Pyrian · Jul 10, 2014 at 04:47 AM

If all you want is to stop moving when you hit a wall, but you're otherwise happy with your movement code, consider using Triggers. Then, you can detect the "collision" in kinematic (4.5+) without any real physics going on. Keep in mind that you'll overlap, so when you catch the trigger, you'll have to back up slightly.

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 as42 · Jul 10, 2014 at 05:27 AM 0
Share

Thanks! Are you saying that when kinematic=true, even though OnCollision won't be called, OnTrigger still will be?

avatar image Pyrian · Jul 10, 2014 at 05:30 AM 0
Share

Yes - in v4.5+. It isn't called in 4.3.

avatar image as42 · Jul 11, 2014 at 12:20 AM 0
Share

Pyrian, while I have you here, one more relevant question:

If Rigidbodies exist to apply forces to gameobjects, and enabling is$$anonymous$$inematic disables all forces from interacting with the object, then... what's the point of a Rigidbody with is$$anonymous$$inematic set to true?

avatar image Pyrian · Jul 11, 2014 at 02:55 AM 0
Share

As opposed to simply not having a Rigidbody2D? If you want to move a collider, you should have a Rigidbody2D - any colliders not associated with a Rigidbody2D are considered static colliders, and there is a high overhead cost with moving them (I imagine this is because of the way they're indexed?).

Also, the kinematic setting is useful for triggering physics on and off for an object without the overhead of adding/removing the component.

If the object has no colliders or does not move, nor is expected to ever interact with physics forces, then a kinematic Rigidbody2D would be entirely superfluous.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Is there a solution to when colliders bypass? 2 Answers

Simple Movement Game: Physics vs Manual Collision Detection? 2 Answers

Unity 4.3 CharacterController collision with physics 2D not working 1 Answer

Rigidbody and Parenting - Kinematic Rigidbodies? 2 Answers

[SOLVED] Desactivate pushing forces between two objects 2 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