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 /
avatar image
0
Question by Jamiemon · Dec 12, 2018 at 07:28 PM · collisioncollision detectioncollision2dclipping

Player passing through wall when pushed 2d C#

I have a game where you move around multiple box players {all attached with the same script} at the same time using the arrow keys {0 gravity scale}. You solve puzzles by getting them in different positions by displacing them with walls, etc. The problem I'm running into is that if one player square pushes the other into a wall, the box in front gets pushed through the wall completely, regardless of how thin or thick the wall may be. The script I have for movement looks something like this:

 private float moveSpeed = 0.1f;
 void Update () {
         if(Input.GetKey("up") || Input.GetKey("right") || Input.GetKey("down") || Input.GetKey("left")){
             transform.Translate(moveSpeed, 0f, 0f);
         }
         if(Input.GetKey("up")){
             transform.eulerAngles = new Vector3(0, 0, 90);
         }else
         if(Input.GetKey("right")){
             transform.eulerAngles = new Vector3(0, 0, 0);
         }else
         if(Input.GetKey("down")){
             transform.eulerAngles = new Vector3(0, 0, -90);
         }else
         if(Input.GetKey("left")){
             transform.eulerAngles = new Vector3(0, 0, 180);
         }
     }
 // I am aware the player can only move in four directions, this is intentional


To try & remedy this situation, I added a part to the script that detected which side there was collision on & move the box in the opposite direction of it. That looks like this:

 private float topAngle;
     private float sideAngle; 
 
     void Start() {
         Vector2 size = GetComponent<BoxCollider2D>().size;
         size = Vector2.Scale (size, (Vector2)transform.localScale);
         topAngle = Mathf.Atan (size.x / size.y) * Mathf.Rad2Deg;
         sideAngle = 90.0f - topAngle;
         Debug.Log (topAngle + ", " + sideAngle);
     }
 
 void OnCollisionStay2D(Collision2D coll) {
 //        Debug.Log("hit something");
         Vector2 v = coll.contacts[0].point - (Vector2)transform.position;
 
         if (Mathf.Abs(Vector2.Angle(v, Vector3.up)) <= topAngle){
 //            Debug.Log("Collision on top");
             transform.Translate(0f, -moveSpeed, 0f);
         }
         else if(Mathf.Abs(Vector2.Angle(v, Vector3.right)) <= sideAngle){
 //            Debug.Log("Collision side1");
             transform.Translate(-moveSpeed, 0f, 0f);
         }
         else if(Mathf.Abs(Vector2.Angle(v, Vector3.left)) <= sideAngle){
 //            Debug.Log("Collision side2");
             transform.Translate(moveSpeed, 0f, 0f);
         }
         else{
 //            Debug.Log("Collision on bottom");
             transform.Translate(0f, moveSpeed, 0f);
         }
     }

Once I did this, the problem happens less often but still consistently enough that it's unplayable. Anyone know what I can do? As always, all help is appreciated <3

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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Sprite ignoring collison 1 Answer

if statement not working when detecting collision between two prefabs 1 Answer

How to set collision for an object with specific collider size? 3 Answers

Disable collision before collision actually happens 1 Answer

2d Collision not detect player when it falls 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