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 Advigames · Feb 06, 2021 at 04:42 AM · 2d collision

can i 2d wall collision without major changes?

im in the very early stages of making a 2d RPG and i want my 2d player(only a sprite but has been given a 2d box collider and 2d rigidbody) to stop moving once he touches anything on he touches, i havent used unity in this scope in 4 years and dont retain the knowledge on it that i had, i want the following code to incorperate collision with as little change to current formula as possible:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerAI : MonoBehaviour
 {
     private int Playerlevel;
     private float speed = 0.1f;
     private Vector2 playerpos;
     private Rigidbody2D player_rb;
 
     // Start is called before the first frame update
     void Start()
     {
         //player_rb = GetComponent<Rigidbody2D>();
         playerpos = transform.position;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKey(KeyCode.W)) playerpos.y += speed; // y++
         if (Input.GetKey(KeyCode.A)) playerpos.x -= speed; // x--
         if (Input.GetKey(KeyCode.S)) playerpos.y -= speed; // y--
         if (Input.GetKey(KeyCode.D)) playerpos.x += speed; // x++
         transform.position = playerpos;
     }
 
     void OnCollisionEnter2D(Collision2D collision)
     {
     }
 
     void OnGUI()
     {
         GUI.Label(new Rect(0, 45, 200, 50), playerpos.x.ToString() + ", " + playerpos.y.ToString());
     }
 }
 

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

Answer by RodrigoAbreu · Feb 07, 2021 at 06:34 AM

Hey @Advigames, here is how you should do it.

 using UnityEngine;
 
 public class PlayerAI : MonoBehaviour
 {
     private int Playerlevel;
     private float speed = 100f;
     private Vector2 playerpos;
     private Rigidbody2D player_rb;
 
     void Start()
     {
         player_rb = GetComponent<Rigidbody2D>();
     }
     
     public void FixedUpdate()
     {
         player_rb.velocity = new Vector3(Input.GetAxisRaw("Horizontal") * speed * Time.fixedDeltaTime, 
                                          Input.GetAxisRaw("Vertical") * speed * Time.fixedDeltaTime);
     }
 
     void OnCollisionEnter2D(Collision2D collision)
     {
         player_rb.velocity = Vector2.zero;
     }
 
     void OnGUI()
     {
         GUI.Label(new Rect(0, 45, 200, 50), playerpos.x.ToString() + ", " + playerpos.y.ToString());
     }
 }


This is the Player Setup:

alt text


For the wall you just need to add a BoxCollider2D

Remember to lock your rotation constraints Z on your Player's rigidBody2D


playersetup.png (31.5 kB)
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 logicandchaos · Feb 06, 2021 at 01:47 PM

if you have a collision box and rb on your character already, you just need to put collision on your walls, no coding needed.

You can also do this:

 void OnCollisionEnter2D(Collision2D collision)
 {
       rb.velocity=Vector2.zero;
 }

Comment
Add comment · Show 4 · 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 Advigames · Feb 06, 2021 at 07:53 PM 0
Share

i tried adding that code and nothing happened, also my objects that i want the player to collide into already have a 3d box collider and 2d rigidbody. do i have to completely rework how the inputs move the player?

avatar image Advigames · Feb 07, 2021 at 04:32 AM 0
Share

i meant a 2d box collider

avatar image RodrigoAbreu Advigames · Feb 07, 2021 at 04:50 AM 0
Share

@Advigames It will not work, because you're not moving your character with velocity. You need to actually make sure your walls have a collider on them, they won't need a rigidbody. Also make sure all your colliders are not marked as "trigger".

avatar image Advigames RodrigoAbreu · Feb 07, 2021 at 05:29 AM 0
Share

okay, so how do i edit my code so i can move my character with velocity?

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

113 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

Related Questions

Is the 2D physics collision matrix different? 2 Answers

OnTrigger and Input.getkey on picking up weapons from the ground. 2 Answers

2D COLLISION NOT WORKING WITH TAGS 3 Answers

How to destroy two gameobjects when they collide? 3 Answers

OnCollisionStay2D is ignoring collisions 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