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
0
Question by Fixe96 · Mar 02, 2017 at 04:22 PM · unity 52d game2d-physicstop-downtop down

[2D] Make player push object

Hello to everyone!

I am working on a top down 2D game and I need your help with pushing an object.

My player, which is a monster, should push a box when it collides with it. I tried several solutions, only one works for me but it works not smooth.

Here is my code for the box:

 private void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player") 
         {
             this.direction = other.GetComponent<Monster> ().direction; 
             transform.Translate (other.GetComponent<Monster>().direction * other.GetComponent<Monster>().speed * Time.deltaTime);
         }
  
     }

The player has the Monster.cs script on it an has a given direction, saved in direction.

What am I doing wrong? Or is there a better solution to push an object in a top down 2D game?

Thanks in advance!

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

Answer by AMU4u · Mar 02, 2017 at 04:53 PM

Make a specific collider for those objects or maybe ALL objects (eh?) that is only for pushing/physics.

Right now if you have trigger colliders , they won't push each other around. Add another one, go into edit>project settings>physics and add a layer that only touches itself. Put colliders on your objects that will be pushed and add the collider to the physics layer.

For this to work, the object to be pushed needs a rigidbody attached to it.

EDIT - For people using raycasting to triggers with no rigidbodies for their colliders/collisions etc, simply go into edit>Project Settings>Physics and check the box that says, "Queries Hit Triggers". You will raycast to triggers and move them as if rigid bodies were involved.

Comment
Add comment · Show 3 · 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 Fixe96 · Mar 02, 2017 at 05:02 PM 0
Share

Thank you!

I did the following: - deleted the script (with the code above) from the box; because it is not necessary anymore? - created a new layer named "pushable", it touches only itself - made a collider (no material) for the player and also a collider (no material) for the box - then I gave the player and box the same layer

But it doesn't work, I think i misunderstood something?

avatar image AMU4u Fixe96 · Mar 02, 2017 at 05:07 PM 0
Share

I should have asked, how is the character movement handled?

avatar image Fixe96 AMU4u · Mar 02, 2017 at 05:10 PM 0
Share

I checked your answer again and unchecked the arrow at "is trigger". Now it works quite nice! Thank you.

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

141 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 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 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 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

[2D] Slide object after collision 2 Answers

Objects are sliding when one is placed on top of the other in a top down game 0 Answers

Player can only jump once!? 0 Answers

Unity 2D: Interpolate object collision stuck 1 Answer

2D Android Game Rapid Jumping Problem,Unity 2018.2.9f1 Android 2d Speed Jumping 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