Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Calcifer21 · Feb 27, 2017 at 02:34 PM · c#collisioncharactertilemapcharacter movement

2D Tile Based collision problem

Okay, so basically I'm trying to make a game similar to the early Ultima's (I-V) I tried to implement a basic code I found on the forums and it works Perfectly! except for one big problem; collisions aren't being detected on the -x and +y planes (Left and Up?) and the collision boxes for the player and the walls seem to get stuck in each other; rendering my player completely stuck, with restarting being the only solution: I've tried plenty of fixes such as changing the material of the wall colliders to set the friction to 0, or using an edge collider instead of a boxcollider2d but none of these have worked; help is greatly appreciated. My Code:

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 using System.Collections;
 
 public class Movementy : MonoBehaviour
 {
 
     public float speed = 2.0f;
     Vector3 pos;
     Vector3 old;
     Transform tr;
 
     void Start()
     {
         pos = transform.position;
         tr = transform;
     }
 
     void FixedUpdate()
     {
         if (Input.GetKey(KeyCode.UpArrow) && tr.position == pos)
         {
             pos += (Vector3.up) / 1;
         }
         else if (Input.GetKey(KeyCode.RightArrow) && tr.position == pos)
         {
             pos += (Vector3.right) / 1;
         }
         else if (Input.GetKey(KeyCode.DownArrow) && tr.position == pos)
         {
             pos += (Vector3.down) / 1;
         }
         else if (Input.GetKey(KeyCode.LeftArrow) && tr.position == pos)
         {
             pos += (Vector3.left) / 1;
         }
 
         transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
     }
 
     void OnCollisionEnter2D()
     {
         float x = transform.position.x;
         float y = transform.position.y;
         float z = transform.position.z;
 
         if ((int)(x + 0.5) > (int)x && x > 0)
             x = (int)x + 0.5f;
         else if ((int)(x - 0.5) < (int)x && x < 0)
             x = (int)x - 0.5f;
         else
             x = (int)x;
 
         if ((int)(y + 0.5) > (int)y && y > 0)
             y = (int)y + 0.5f;
         else if ((int)(y - 0.5) < (int)y && y < 0)
             y = (int)y - 0.5f;
         else
             y = (int)y;
 
         z = 0f;
 
         Vector3 away = new Vector3(x, y, z);
         pos = away;
 
     }
 }
 



Comment
Add comment · Show 3
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 NoseKills · Feb 27, 2017 at 04:55 PM 1
Share

The code is pretty simple. Should be really easy to use Debug.Log to print out the values and see which if-branch you end up in which case.

At least keep in $$anonymous$$d that

 (int)0.6f + 0.5f; // result it 0.5 float
 (int)(0.6f + 0.5f) // result 1 int
avatar image Calcifer21 NoseKills · Feb 28, 2017 at 03:20 AM 0
Share

Hello, after adding the following lines of code to the script I get these messages in the console, I believe the problem to lie in the script only accepting movement on a positive or negative direction. A fix for this would be greatly appreciated alt text

Thanks for replying!

screenshot-6.png (345.9 kB)
avatar image ninja_gear · Feb 28, 2017 at 05:02 AM 0
Share

Part of the problem is in the OnCollisionEnter2D:

x + 0.5f > x

Will always return true so there is no need to check for that. Same goes for 'x - 0.5f < x'. I tried my hardest to reproduce this bug, but alas, I could not get OnCollisionEnter2D() to fire...

$$anonymous$$y suggestion to make things simpler would be for you to try to use a CharacterController Component, as it has build in collision detection built into its move functions.... IIRC. Sorry I couldn't offer anymore help. Good luck!

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

9 People are following this question.

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

Related Questions

Physics2D.OverlapBox not working as expected. 1 Answer

Distribute terrain in zones 3 Answers

How can I have a CharacterController's height make it to where the feet are correctly on the ground? 0 Answers

Help please in regards to character controller 0 Answers

How not to penetrate the walls? 3 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