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
0
Question by Wispy · May 24, 2015 at 02:38 PM · 2dcollisiontopdowntop-down

Simple top-down 2D collision

I'm trying to build my first game using the 2D features in Unity. I've got character movement working for now, but I'm having trouble implementing player collision with the blocking layer (walls, etc). This is the code I've tried so far:

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
 
     public float Speed = 2.0f;
     public LayerMask BlockingLayer;
 
     private BoxCollider2D _BoxCollider;
     private Rigidbody2D _Rigidbody;
     private Animator _Animator;
 
     // Use this for initialization
     void Start () 
     {
         this._BoxCollider = this.GetComponent<BoxCollider2D>();
         this._Rigidbody = this.GetComponent<Rigidbody2D>();
         this._Animator = this.GetComponent<Animator> ();
     }
     
     // Update is called once per frame
     void Update () 
     {
         int horizontal = (int) Input.GetAxisRaw("Horizontal");
         int vertical = (int) Input.GetAxisRaw ("Vertical");
 
         if (vertical != 0)
         {
             horizontal = 0;
         }
         if (horizontal != 0 || vertical != 0)
         {
             this.Move (horizontal, vertical);
         }
         else
         {
             this.Idle();
         }
     }
 
     private void Move(int horizontal, int vertical)
     {        
         this.AnimateMovement(horizontal, vertical);
         this.Move(new Vector2(horizontal, vertical));
     }
 
     private void Move(Vector2 direction)
     {        
         /*if (this._BoxCollider.IsTouchingLayers(this.BlockingLayer))
         {
             return;
         }*/
         
         //this._Rigidbody.velocity = direction * this.Speed * Time.deltaTime;
 
         Vector2 start = this.transform.position;
         Vector2 end = start + direction * this.Speed * Time.deltaTime;
         RaycastHit2D hit = Physics2D.Linecast (start, end, BlockingLayer);
 
         if (this._BoxCollider.IsTouchingLayers(this.BlockingLayer) && null != hit)
         {
             return;
         }
 
 
         Vector3 newPosition = new Vector3(end.x, end.y, 0f);
         this._Rigidbody.MovePosition (newPosition);
         /*if (this._BoxCollider.IsTouchingLayers(this.BlockingLayer))
         {
             this._Rigidbody.MovePosition (start);
         }*/
     }
 
     void OnCollision2D(Collision2D collision)
     {
         /*if (collision.gameObject.layer == this.BlockingLayer)
         {
             this._Rigidbody.velocity = Vector2.zero;
         }*/
     }
 
     void OnTriggerEnter(Collider other)
     {
         //if (collision.gameObject.layer != this.BlockingLayer)
         //{
         //    return;
         //}
         //this._Rigidbody.velocity = Vector2.zero;
     }
 
     void OnTriggerStay(Collider collider)
     {
         //this._Rigidbody.velocity = Vector2.zero;
     }
 
 
     private void AnimateMovement(int horizontal, int vertical)
     {
         int direction = 0;
         if (vertical > 0)
         {
             direction = 2;
         }
         else if (vertical < 0)
         {
             direction = 4;
         }
         else if (horizontal > 0)
         {
             direction = 1;
         }
         else if (horizontal < 0)
         {
             direction = 3;
         }
         this._Animator.SetInteger ("Direction", direction);
     }
 
     private void Idle()
     {
         this._Rigidbody.velocity = Vector2.zero;
         this._Animator.SetInteger("Direction", 0);
     }
 }
 

Both the character and the walls have both a kinematic Rigidbody2D and a BoxCollider2D. Currently what happens is that the player is correctly blocked by walls, but then they are no longer able to move at all after the collision. Is there a standard solution for doing this sort of collision?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Cherno · Jun 23, 2015 at 10:03 AM

Is there any reason why you are using kinematic rigidbodies? These only register trigger collisions and don't block movement correctly. Your code behaves as expected: If a trigger (wall collider) is touched, the rigidbody completely stops moving. What should probably happen is that it can't go any further into the wall, but otherwise move in any direction, including "scraping" along the wall. To achieve this, set the rigidbodies to be not kinematic, and their colliders to not be triggers (player and walls). Also, it's uncommon to let the walls, or any other static colliders, have rigidbodies. Normally, a collider for such objects is enough. Only if the wall itself should behavor physically realistic does it really need a rigidbody, but since it should just sit there and block collisions by actors, that's not needed and also helps performance.

So, basically... what lloladin said ;)

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

Answer by lloladin · Jun 23, 2015 at 09:06 AM

you acheiv this without any code really by just applying a rigibody and 2collider to youre player and boxcollider to youre wall

here is a link where someone explains how to make a 2d game really well

https://www.youtube.com/watch?v=86Bgt--Ww7w&list=PLiyfvmtjWC_Up8XNvM3OSqgbJoMQgHkVz

Comment
Add comment · Show 2 · 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 sbloom85 · Apr 27, 2016 at 04:53 PM 0
Share

That covers platformers , not top-down games.

avatar image Cherno sbloom85 · Apr 27, 2016 at 05:21 PM 0
Share

Shouldn't matter because the only things different are gravity and axis orientation.

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

6 People are following this question.

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

Related Questions

Display 1 No cameras rendering when player collides with enemy 1 Answer

2D Top Down Shooting Problem 1 Answer

How to have camera follow player unless it hits a boundary 1 Answer

8-directional orientation, top down 2D, seperate from movement 0 Answers

Lines are disappearing and merging as I zoom out 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