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 cecilfuel95 · Nov 13, 2015 at 09:50 PM · 2dmovementcollision detectiontop downarrow-keys

Top Down 2D: How to make a game perimeter that keeps objects inside

I'm attempting to make a (very simple to begin with) top-down game in 2D wherein the player (using arrow keys) is able to move around an enclosed polygonal area of the game. I'm having trouble ensuring that all parts of the area's perimeter (made of a polygonal collider) are impenetrable (areas near corners tend not to be for some reason). Code below:

 public float speed = 40f;
 public bool heTouchedTheButt = false; //test whether the player is colliding with the perimeter

 void OnCollisionEnter2D(Collision2D coll) {

     if (coll.gameObject.name == "Perimeter")
         heTouchedTheButt = true;
 }

 void OnCollisionExit2D(Collision2D coll) {
     
     if (coll.gameObject.name == "Perimeter")
         heTouchedTheButt = false;
 }

 void FixedUpdate (){
     if (!heTouchedTheButt) {
         if (Input.GetKey (KeyCode.LeftArrow)) {
             transform.position += Vector3.left * speed * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.RightArrow)) {
             transform.position += Vector3.right * speed * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.UpArrow)) {
             transform.position += Vector3.up * speed * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.DownArrow)) {
             transform.position += Vector3.down * speed * Time.deltaTime;
         }
     } else if (heTouchedTheButt) {
         if (Input.GetKey (KeyCode.RightArrow)) {
             transform.position += Vector3.left * speed * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.LeftArrow)) {
             transform.position += Vector3.right * speed * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.DownArrow)) {
             transform.position += Vector3.up * speed * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.UpArrow)) {
             transform.position += Vector3.down * speed * Time.deltaTime;
         }
     }
 }

I figured setting the keys to be inversed when the player is in contact with the perimeter would allow them to come out of contact, but this appears to work only some of the time. Other times, the controls get stuck in the inverse and others allow the player to go right past the boundary as if it weren't there. I feel like there's a more efficient way to set up this boundary, and I appreciate any insight on how to make this easier.

Comment
Add comment · Show 1
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 killerstreak · Nov 19, 2015 at 12:25 PM 0
Share

does the player have a collider? I'm no 100% sure what you're saying, you mean two collider can pass through each other? because that shouldnt normally happen.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jay-Pickle · Nov 19, 2015 at 01:08 PM

Hi cecilfuel95,

You are probably encountering this problem because you are assigning transform.position. This is actually more like teleporting rather than moving. So if transform.position += Vector3.down * speed * Time.deltaTime happens to be on the other side of the collider, Unity dosent have a problem teleporting it to that position.

If I was you, I would give the game object your moving a Rigidbody or Rigidbody2D. And then do something like:

 public float speed;

 private Rigidbody2D rigidbody;

 private void Awake()
 {
     rigidbody = GetComponent<Rigidbody2D>();
 }

 private void FixedUpdate()
 {
     if(Input.GetKey(KeyCode.RightArrow))
     {
         rigidbody.velocity = (Vector2.right * speed);
     }
 }

Since this involves physics, use FixedUpdate(). You don't have to worry about Time.deltaTime here. Let me know if that works out for you.

Comment
Add comment · Show 3 · 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 cecilfuel95 · Nov 19, 2015 at 06:16 PM 0
Share

Thanks, @$$anonymous$$ Pickle! Your insight was very helpful and I was able to correct my problem easily. If you have any tips on my next problem, I would appreciate the help. Now I'm working on making the player teleport between scenes to the same position (the different scenes representing different storeys in a building and the spot of teleportation representing stairs from one floor to the next). The "stairs" act as a trigger which loads the scene the player intends to go to, but I haven't been able to make it so that when the stairs are triggered and the next scene is loaded, that the player appears in the same position they were in when they triggered the stairs. Ins$$anonymous$$d, the player goes to (0,0) by default. Should I get the player's position first thing when the stairs are triggered, then load scene, then apply the saved position to the player? Thanks!

avatar image Jay-Pickle cecilfuel95 · Nov 19, 2015 at 06:41 PM 0
Share

Hi cenifuel95,

I can't think of anything off the top of my head, I suggest posting a new thread with that question.

avatar image cecilfuel95 · Nov 19, 2015 at 06:51 PM 0
Share

Thanks, @$$anonymous$$ Pickle! Your insight was very helpful and I was able to correct my problem easily. If you have any tips on my next problem, I would appreciate the help. Now I'm working on making the player teleport between scenes to the same position (the different scenes representing different storeys in a building and the spot of teleportation representing stairs from one floor to the next). The "stairs" act as a trigger which loads the scene the player intends to go to, but I haven't been able to make it so that when the stairs are triggered and the next scene is loaded, that the player appears in the same position they were in when they triggered the stairs. Ins$$anonymous$$d, the player goes to (0,0) by default. Should I get the player's position first thing when the stairs are triggered, then load scene, then apply the saved position to the player? Thanks

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Player rotation affecting Top Down 2D Movement 1 Answer

Detect collision with character controllers on the same layer, but allow objects to pass through one another 2 Answers

Top down 2d character controller sometimes walking backwards 1 Answer

Tile based movement collisions 0 Answers

How do I get my character to keep his left and right momentum in the air while also disabling movement controls until grounded? 0 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