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
1
Question by drism · Sep 27, 2012 at 07:23 PM · c#2dcollisionsprite

Implementing 2D Sprite Collisions

I'm currently working on a 2d sprite based game (using spritemanager 2) with an overworld similar to something like Zelda: Link to the past. My plan is to represent the world in layers, with the deepest layer being a large sprite for the background. I'll then have a GameObject layer (with objects that may or may not have a sprite component) that will consist largely of collison boxes for interaction and scripting.

To prototype out this apporoach, I have a background sprite at my deepest z layer. The texture is a simple path through some grass. To prevent the player from leaving the path, I have a couple of GameObjects with collision boxes in my object layer that cover the grass representing walls, that are set to trigger. These invisible walls are static collision areas that should not move, representing areas the player shouldn't be able to reach. Finally, I have a GameObject also on the object layer with a sprite, rigidbody set to kinematic, and a collision box, that represents my player sprite. I'm moving this player simply by translating a given integer amount each update if a particular key is held down.

What I'm trying to do is script in a simple mechanism that will stop the player from moving if they run into the wall, and will prevent them from moving if the direction is held down.

Currently, I have a public method on my player script that translates the exact opposite of what the last movement translation one. This is called from the wall's OnTriggerEnter method. It effectively just bounces my player off the wall, and just feels really broken.

I'm looking for advice on how to implement an algorithm that will just cleanly stop my sprite in its tracks, and keep his collision box just outside the wall if the player continues to move in the direction of the wall. You know,exactly how you would expect this to behave as a player.

Also, would love to hear any comments if there are any obvious flaws to this overall approach, given what I'm trying to make. Thanks for helping a unity newb!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kmeboe · Sep 27, 2012 at 10:25 PM

I'm somewhat of a noob when it comes to Unity physics. However, reading the page for Rigidbody.isKinematic (http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-isKinematic.html), it looks like rigidbody objects with isKinematic set will ignore all of the physics interactions that you desire.

Have you tried making your character's rigidbody not kinematic? This may solve your problem. Is there some reason you want to use a kinematic rigidbody?

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

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I set the position and size of a sprite from inside a script ? 1 Answer

How can I cycle through sprites on button presses (C#)? 1 Answer

Stop 2D camera X axis movement if equal to one of two coordinates 1 Answer

Creating circles using script 1 Answer

How to detect an object inside another object? 1 Answer


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