• 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
Question by amydoopey · Apr 20, 2020 at 07:51 PM · collisionunity 2dcollision detectioncollision2dcollision issues

Unity2D: How to make my player not walk through walls?

Hey, I'm really sorry if this have been answered before, but I tried looking everywhere and I couldn't really find my solution. (I should also mention I'm extremely new to this, I tried looking through the manual, but I couldn't quite find what I did wrong :( )

So I'm working on a 2D game where there is a character (a UI image) that's supposed to not walk into walls (also a UI image).

Here's the code for the character:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class PlayerController : MonoBehaviour
 {
 
     public float speed;
 
     void Update()
     {
         float v = Input.GetAxisRaw("Vertical");
         float h = Input.GetAxisRaw("Horizontal");
         Vector3 direction = new Vector3(h, v, 0f).normalized;
         transform.Translate(direction * speed * Time.deltaTime);
     }
 }

(the speed is set to 80 if that matters)

The player has a circle collider 2d and a rigidbody 2d and the wall has a polygon collider 2d and a rigidbody2d (I had heard that technically a rigidbody wouldn't be necessary if the player has one already, but for some reason it wouldn't work if the wall didn't have any). They don't have any materials attached to them.

So what's the problem? Well, my player does seem to know that the walls are walls, and it slows down as it comes in collision with the wall, but it can still walk through the wall if you keep pressing. I would like for the wall to be completely impenetrable, but I can't seem to make that work :(

Things I've tried: -Making sure that none of the box collider have IsTrigger checked (they don't). -Using OnCollisionEnter2D to bring the speed to 0 once they come into collision (the speed does go to 0, but then it stays that way so the player can't move anymore, which is not what I want).

What can I do to make the walls completely impenetrable? Thank you so much in advance and I'm sorry if this is long, I just wanted to make sure this was detailed (I can add screenshots of the components if necessary, there's just an image limit here haha).

Comment

People who like this

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

Answer by jkpenner · Apr 21, 2020 at 03:46 AM

Since your character has a rigidbody attached you will need to move the character through it's rigidbody and not it's transform. You will need to get a reference to the rigidbody attached to the character, then in your update you would used the rigidbody's MovePosition() method. In the end you should have something that looks like this:

 this.rg2d.MovePosition(this.rg2d.position + (direction * speed * Time.deltaTime));


Rigidbody's MovePosition Documentation

Comment
amydoopey
whatsagime

People who like this

2 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 amydoopey · Apr 21, 2020 at 06:20 PM 0
Share

Oooh I see, thank you so much! Unfortunately, I still can't get to work haha. This is what the code looks like right now. public class PlayerController : MonoBehaviour {

     public Vector2 speed;
     private Rigidbody2D rb2d;
 
     void Update()
     {
         rb2d = GetComponent<Rigidbody2D> ();
     }
 
     void FixedUpdate()
     {
         this.rb2d.MovePosition(this.rb2d.position + (speed * Time.fixedDeltaTime));
     }
 }

The player doesn't move and it just automatically glides to the wall right next to him :( I'm pretty sure I made an obvious error somewhere but I can't quite find it. I tried basing it off of the one in the manual.

avatar image amydoopey amydoopey · Apr 21, 2020 at 06:39 PM 0
Share

Never mind I figured it out and it looks like this now and it works public class PlayerController : MonoBehaviour { public Vector2 speed; private Rigidbody2D rb2d;

     void Update()
     {
         rb2d = GetComponent<Rigidbody2D> ();
     }
 
     void FixedUpdate()
     {
         float v = Input.GetAxisRaw("Vertical");
         float h = Input.GetAxisRaw("Horizontal");
         Vector2 direction = new Vector2(h, v).normalized;
         rb2d.MovePosition(rb2d.position + (direction * speed * Time.fixedDeltaTime));
     }
 }

Thank you so much! :D

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

How to change collision for just part of an object? (similar to sprite mask) 0 Answers

If gameobject on collision with gameobject then destroy 0 Answers

collisions does not get detected every time 0 Answers

Two different spheres attached to one object for collisions 0 Answers

if statement not working when detecting collision between two prefabs 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