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 Vehrmt · Sep 27, 2013 at 10:42 AM · collisionmovement scriptorthello

Diagonal collision script not working (Unity + Orthello, C#)

I recently started coding in Unity after being introduced to the Orthello framework for 2D games, and ran into a problem I can't figure out regarding diagonal movement in a grid. I do not believe that the Orthello framework is what's giving me headaches, however. I'm using a modified version of this script, made by Eric Haines. The movement is grid-based, with each tile being 64x64.

On each update, collision raycasts are recalculated. This gives me a few variables that let me know if my character can go in any cardinal direction. The raycasts only negate booleans, while moving in directions opposite of negated booleans re-enables them.

For example:

 if (Physics.Raycast (new Vector3(sprite.position.x, sprite.position.y, 0), Vector2.up-Vector2.right, out hit, 64))
         {
             moveNW = false;
         }
         else moveNW = true;


This code works for vertical and horizontal movement:

 if(input.y>0 && moveUp){ moveDown = true; sprite.Resume(); if (!isMoving)StartCoroutine(move(transform)); }
 
 else if(input.y<0 && moveDown){ moveUp = true; if (!isMoving)StartCoroutine(move(transform)); sprite.Resume();}
 
 else if(input.x<0 && moveLeft){ moveRight = true; sprite.Resume(); if (!isMoving)StartCoroutine(move(transform));}
 
 else if(input.x>0 && moveRight){ moveLeft = true; sprite.Resume(); if ( !isMoving)StartCoroutine(move(transform));}
 

So, I always have 8 booleans telling me which directions I can go at any given time. When I try to move diagonally however, the collision detection fails in some cases. When not in a corner, the character clips through the collidables. When in a corner, for some reason, the character stays put if I try to send it through the corner collidable.

Here's the code I'm using for diagonal movement (the raycasting part works correctly, I checked the truth values of the variables through the debugger):

             if(input.y>0 && input.x>0 && moveNE){moveSE = true; sprite.Resume (); StartCoroutine(move(transform));}
             else if(input.y>0 && input.x<0 && moveNW){moveSW = true; sprite.Resume (); StartCoroutine(move(transform));}
             else if(input.y<0 && input.x<0 && moveSW){moveNE = true; sprite.Resume ();StartCoroutine(move(transform));}
             else if(input.y<0 && input.x>0 && moveSE){moveNW = true; sprite.Resume ();StartCoroutine(move(transform));}

I'd appreciate any insight more experienced developers can offer about this problem. Again, I mention that the truth variables are correct, as well as the input values.

For possible relevance to the problem, here's the movement function as well, which is a simplified version of Eric's script:

  public IEnumerator move(Transform transform) {
         isMoving = true;
         startPosition = transform.position;
         t = 0;
 
             endPosition = new Vector3(sprite.position.x + System.Math.Sign(input.x) * gridSize,
                 sprite.position.y + System.Math.Sign(input.y) * gridSize, 0);
             factor = 1f;
         
         while (t < 1f) {
             t += Time.deltaTime * (moveSpeed/gridSize) * factor;
             sprite.position = Vector3.Lerp(startPosition, endPosition, t);
             yield return null;
         }
         isMoving = false;
         yield return 0;
     }

Before you mention it, I'm aware that simply raycasting between my sprite's point of origin and the destination on the grid should make the problem much easier to solve. However, I would like to understand the error in this method, efficiency aside.

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 Jamora · Sep 27, 2013 at 12:43 PM 0
Share

What caught my eye during a quick skim, was that you use Vector2.up & Vector2.right in your first snippet. They are the global directions, along the world axes. This could be a problem if your transform or playing grid has rotated at all. I would use sprite.up & sprite.right, as they're the directions relative to the actual transform. (Assu$$anonymous$$g sprite references the transform.)

1 Reply

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

Answer by Vehrmt · Sep 28, 2013 at 12:30 AM

Thanks for the feedback, Jamora! I'll look towards using a more unified format if I implement rotation later on.

In the meanwhile, I figured out a solution to my problem. While the diagonal movement conditions were working as expected, my vertical/horizontal if conditions were insufficient. Because I used input.x and input.y by themselves, they didn't restrict input on the other axis, and the movement script triggered.

Here's the edited code for the horizontal/vertical movement conditions:

 if(input.y>0 && (input.x == 0) && moveUp){ moveDown = true; sprite.Resume(); if ( !isMoving)StartCoroutine(move(transform)); }
         else if(input.y<0 && (input.x == 0) && moveDown){ moveUp = true; if (!isMoving)StartCoroutine(move(transform)); sprite.Resume();}
         else if(input.x<0 && (input.y == 0) && moveLeft){ moveRight = true; sprite.Resume(); if (!isMoving)StartCoroutine(move(transform));}
         else if(input.x>0 && (input.y == 0) && moveRight){ moveLeft = true; sprite.Resume(); if (!isMoving)StartCoroutine(move(transform));} 
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

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

16 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

Related Questions

Problem with 2D movement 0 Answers

NullReferenceException with Terrain.activeTerrain 2 Answers

I need some help with my script. Im just a beginner and dont know whats happening. 1 Answer

Game object passes through walls 0 Answers

Glitchy collisions 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