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 /
This question was closed Apr 24, 2015 at 03:35 PM by martin-rohwedder for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by martin-rohwedder · Apr 22, 2015 at 07:55 PM · 2dcollisionspriteboxcollider2dpush

Push only 1 box around at a time (2D)

Hi all

I am in the proces of learning 2D development in unity, and therefore I am creating a Sokoban game, where the player needs to push blocks/boxes around a game level.

I am using a 2D box collider and Rigidbody2D, to push my boxes around. The box is stopping fine when it is pushed into a wall, but when it is pushed into another box, then the new box also begins move away, when it collides with the pushing box.

I can try eloborate it further.

  1. Player moves into a box, and the box is starting to move away from the player (the push effect)

  2. The box is pushed into another box

  3. The other box is now also pushed away, so the player is now pushing 2 boxes at the same time.

  4. It's here the problem is, since the player may only push one box at a time.

Please help me with how I can achieve that effect.

alt text

rigid.png (18.4 kB)
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

  • Sort: 
avatar image
1
Best Answer

Answer by acorrow · Apr 22, 2015 at 08:25 PM

Can you just make them Kinematic and only "awake" them when teh player is colliding/touching them? Something like this... Just have it set it back on the CollisionExit2D event as well...

 private Rigidbody2D body;
         void Start()
         {
         body = GetComponent<Rigidbody2D> ();
         }
 
         void OnCollisionEnter2D(Collision2D coll) {
         if (coll.gameObject.tag == "Player")
             body.isKinematic = false;        
         }

Hope that Helps!

Adam

Comment
Add comment · Show 6 · 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 martin-rohwedder · Apr 22, 2015 at 08:35 PM 0
Share

When I try to do that, the OnCollisionExit2D() method is executing directly after the player has collided, and not when he is moving away, so the box is not moving at all... How can I solve that, is it something with the box colliders?

avatar image acorrow · Apr 22, 2015 at 08:41 PM 0
Share

No nothing wrong with them. You said push not pull ;) $$anonymous$$oving away would be pulling. In that case you may want to try a different approach, possibly parenting the box to the player transform, or something to that effect.

avatar image martin-rohwedder · Apr 23, 2015 at 05:55 PM 0
Share

$$anonymous$$hh.. I am not sure I understand what you mean... What I mean by pushing is like the logic in this video - https://www.youtube.com/watch?v=TgWibgp2Nw$$anonymous$$

avatar image acorrow · Apr 23, 2015 at 06:32 PM 1
Share

Ok I see what you mean.

Colliders may not be what you want to use here. Try ins$$anonymous$$d using Raycasting to deter$$anonymous$$e where you are in relation to the crate/box to "activate" it (set is kinematic = false) and allow your characters rigidbody to move it once close enough.

So for example, you will want to do a very similar thing to using OnCollisionEnter2D, but just cast a ray and see if that hit:

         RaycastHit2D hit = Physics2D.Raycast(frontOf$$anonymous$$yCharacter.transform.position, directionToCastRay, distanceToCastRay, Layer$$anonymous$$askOfWhatToCollideWith);
 
         if (hit)//you hit your crate object
         {
             hit.rigidbody.is$$anonymous$$inematic = false;
         }
 

This casts a ray, for however long you want, in a direction, and if it collides with the box, sets the box to be not kinematic.

Or you could do the opposite and have the crate raycast on all 4 sides "looking" for the player so that you can make it not kinematic.

Hope that Helps!

Adam

avatar image martin-rohwedder · Apr 23, 2015 at 08:26 PM 1
Share

Thank you, I got it working now. I have made my code like this, which is attached to each of the boxes

 using UnityEngine;
 using System.Collections;
 
 public class BoxCollisionScript : $$anonymous$$onoBehaviour {
 
     //public float movementSpeed = 1.5f;
     private Rigidbody2D rb2D;
 
     // Use this for initialization
     void Start () {
         rb2D = GetComponent<Rigidbody2D>();
     }
     
     // Update is called once per frame
     void Update () {
         CheckForPlayerHit();
     }
 
     //Check if the player is hitting a box, from one of its four sides. Set the box is kinematic to false, if player hits.
     void CheckForPlayerHit() {
         RaycastHit2D hitUp = Physics2D.Raycast (transform.position + new Vector3(0.0f, 0.7f, 0.0f), Vector2.up);
         RaycastHit2D hitDown = Physics2D.Raycast (transform.position + new Vector3(0.0f, -0.7f, 0.0f), -Vector2.up);
         RaycastHit2D hitRight = Physics2D.Raycast (transform.position + new Vector3(0.7f, 0.0f, 0.0f), Vector2.right);
         RaycastHit2D hitLeft = Physics2D.Raycast (transform.position + new Vector3(-0.7f, 0.0f, 0.0f), -Vector2.right);
         
         if (hitUp.collider.gameObject.tag == "Player" || hitDown.collider.gameObject.tag == "Player" || 
             hitRight.collider.gameObject.tag == "Player" || hitLeft.collider.gameObject.tag == "Player")
         {
             rb2D.is$$anonymous$$inematic = false;
         }
         else
         {
             rb2D.is$$anonymous$$inematic = true;
         }
     }
 }
 
Show more comments

Follow this Question

Answers Answers and Comments

20 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

Related Questions

2D box colliders not touching but are colliding, how to fix? 0 Answers

Sprite collision with Tilemap 2 Answers

How do you create complex collision geometry to match 2D sprites? 2 Answers

How do I check if/where in a sprite another sprite is? 0 Answers

Changing sprites from spritesheet on collision. What am I doing wrong? 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