Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by Akkronym · Jan 30, 2016 at 05:16 AM · rigidbody2dcollision detectionmovement scriptkinematicontriggerenter2d

Prevent Player from passing through walls

I swear I've looked all over for solutions to this problem before posting. I know the solution exists somewhere because this appears to be a common problem and it can't possibly have an incredibly complex solution, but I just haven't found it yet and I need help.

I'm making a 2d game in Unity. I have two "Sprite" game objects, one that represents the player with a circle collider, a kinematic rigidbody2D and one that is an obstacle with a square collider. My intent is for the

I'm moving the player via transform.position by getting input from the arrow keys to determine a destination vector and Lerping from the current position towards the intended direction. Movement through this method works exactly as intended on its own.

When the player object encounters any other game object, it passes through as though the obstacle is in the foreground and the player is in the background. These obstacles are intended to be static colliders.

Based on Unity's manual, a kinematic rigidbody doesn't provide a collision with a static collider, but it does produce a trigger. However, when the obstacles are set to "Is Trigger," the player still passes through. All objects have the same Z values (they aren't actually passing underneath each other).

I can make them collide if I give the obstacles rigidbodies, but then they are no longer stationary. They are effected by gravity and are moved when the player runs into them. Setting gravity scale to 0 fixes the gravity problem, but no matter how high the mass is, the player still moves them on contact. If make the obstacle's rigidbody2D kinematic, then it goes back to not colliding.

So I seem to have one of two problems:

  1. How do I use OnTriggerEnter2D to enforce the boundaries of an object programmatically?

  2. How do I move the player as a non-kinematic, totally regular rigidbody2D with the arrow keys based on player input?

I don't know which is actually my problem or how to solve either one.

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
1
Best Answer

Answer by Trigary · Jan 30, 2016 at 07:01 AM

Triggers aren't colliders. Think of them as lasers. If something steps into them, they will detect it, but won't stop it from colliding. You only need basic colliders to solve your issue.

You seem to move the object by modifying its position, right? You don't want to do that, physics don't like it. You can add a force to the object to make it move (AddForce), or you can set a velocity vector (Vector2 velocity). I think the second one is the easier, but some people say it's not correct to modify the velocity of an object directly, only in some cases, but feel free to use it :)

Here's a script :

Put this below MonoBehaviour:

 private Rigidbody2D PlayerObject;

This goes to the "Start" "section":

 PlayerObject = GetComponent<Rigidbody2D>();

This to the FixedUpdate:

 float inputX = Input.GetAxisRaw ("Horizontal");
 float inputY = Input.GetAxisRaw ("Vertical");
 //GetAxisRaw is 0 or 1, GetAxis changes from 0 to 1, from 1 to 0, not instantly.
 
 if (inputX != 0 && inputY != 0) {
      PlayerObject.velocity = transform.up * speed.y * inputY + transform.right * speed.x * inputX;
 }


Ooups, forgot the arrow keys, here you go: http://docs.unity3d.com/ScriptReference/Input.GetKey.html

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

41 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbody2D.MovePosition() doesn't do anything (but animations and transform.Translate() works) 0 Answers

Speculative CCD without rigidbody 1 Answer

Pick up code issue 1 Answer

How do I keep rigidbodies from pushing each other? 2 Answers

Limiting backwards speed ,Limiting backwards speed 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