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
1
Question by SanSolo · Sep 10, 2015 at 07:38 AM · unity5

[2D] Player movement not working against diagonal elements

I am building a game like The line Zen. Player is moving along a road and has to avoid hitting the edges. Right now, i have planned it as follows: On click , cast rays in 8 directions. For the returned hit, perform movement. For e.g, if ray hits something on the right, and tag of that collider is "up", player moves up & left (-1,1). If tag is "down", player moves left and down (-1,-1).

Road objects have tags "up" and "down" to indicate whether player should move up or down on that road.

This is working on horizontal and vertical roads shown here: alt text

But, does not work on diagonal roads shown here: alt text

Code shown below: public class PlayerController : MonoBehaviour { float distance =0.5f; public LayerMask lm; RaycastHit2D up,down,left,right,ne,nw,se,sw; Vector2 NE,NW,SE,SW;

     // Use this for initialization
     void Start () {
     
         GetComponent<Rigidbody2D>().velocity=Vector2.right;
 
         NE = new Vector2 (1, 1);
         NW = new Vector2 (-1, 1);
         SE = new Vector2 (1,-1);
         SW = new Vector2 (-1, -1);
 
         
     }
     
     // Update is called once per frame
     void Update () {
     
     
 
         if (Input.GetMouseButtonDown (0))
             castRay ();
 
 
 
     }
 
     void castRay()
     {
 
         up = Physics2D.Raycast (transform.position,Vector2.up,0.5f,lm);
         down = Physics2D.Raycast (transform.position, -(Vector2.up), 0.5f, lm);
         right = Physics2D.Raycast (transform.position, Vector2.right, 0.5f,lm);
         left = Physics2D.Raycast (transform.position, -(Vector2.right), 0.5f, lm);
         ne=Physics2D.Raycast (transform.position, NE, 0.5f, lm);
         nw=Physics2D.Raycast (transform.position, NW, 0.5f, lm);
         se=Physics2D.Raycast (transform.position, SE, 0.5f, lm);
         sw=Physics2D.Raycast (transform.position, SW, 0.5f, lm);
 
 
         if (up.collider != null) {
 
                 GetComponent<Rigidbody2D>().velocity=new Vector2(1,-1);
             }
 
         else if (down.collider != null) {
             GetComponent<Rigidbody2D>().velocity=new Vector2(1,1);
         }
 
         else if (left.collider != null) {
             
             if(left.collider.tag=="up")
                 GetComponent<Rigidbody2D>().velocity=new Vector2(1,1);
 
             else
                 GetComponent<Rigidbody2D>().velocity=new Vector2(1,-1);
 
         }
 
         else if (right.collider != null) {
 
 
             if(right.collider.tag=="up")
             {
                 Debug.Log("right hit");
                 GetComponent<Rigidbody2D>().velocity=new Vector2(-1,1);
             }
             
             else
                 GetComponent<Rigidbody2D>().velocity=new Vector2(-1,-1);
             
         }
 
         else if (ne.collider != null) {
             
             
             if(ne.collider.tag=="up")
             {
                 Debug.Log("ne hit");
                 GetComponent<Rigidbody2D>().velocity=new Vector2(-1,1);
             }
             
             else
                 GetComponent<Rigidbody2D>().velocity=new Vector2(-1,-1);
             
         }
 
         else if (nw.collider != null) {
             
             
             if(nw.collider.tag=="up")
             {
                 Debug.Log("right hit");
                 GetComponent<Rigidbody2D>().velocity=new Vector2(1,1);
             }
             
             else
                 GetComponent<Rigidbody2D>().velocity=new Vector2(1,-1);
             
         }
 
         else if (se.collider != null) {
             
             
             if(se.collider.tag=="up")
             {
                 Debug.Log("right hit");
                 GetComponent<Rigidbody2D>().velocity=new Vector2(-1,1);
             }
             
             else
                 GetComponent<Rigidbody2D>().velocity=new Vector2(-1,-1);
             
         }
 
 
     }
 }

lm is the layermask assigned to "road" layers. How can I make it behave the same way on diagonal roads?

vert.png (625 B)
diag.png (1.6 kB)
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

1 Reply

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

Answer by Suddoha · Sep 10, 2015 at 08:57 AM

I'm afraid your approach is a bit wrong. You'll most-likely always hit something with one of the first raycasts, which doesn't allow the other movements to be executed (the else-if parts).

I personally wouldn't go with raycasts for this, it's too complicated to solve this problem with it. I'd rather suggest to rely on triggers which seem almost perfect for this purpose. They also have several advantages:

  • you can place them where you want

  • no complicated logic to determine which direction to move next, you'll simply check the triggers tag and act accordingly

  • easier to maintain

  • less bugs and less complicated code

  • but most important: much more control about the direction (see below)

For example, if you want to place a diagonal part, simply put a trigger at the start of the area. Once entered, you apply the respective velocity. And the best: You don't have to rely on a complicated raycat logic to be able to set 8 directions, you can set every directional vector that you want. If you need some more info, just write it as a comment.

Comment
Add comment · 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 SanSolo · Sep 10, 2015 at 10:44 AM 0
Share

I arrived at nearly the same idea :)

Ins$$anonymous$$d of raycasts, I will use 4 empty child objects with trigger. With some fine tuning, it should work.

avatar image Suddoha · Sep 10, 2015 at 10:53 AM 0
Share

If I were you i wouldn't attach the triggers to the player object, just associate it with the part that comes next. ;)

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

28 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

Related Questions

No realtime shadows in webplayer build? 0 Answers

How to calculate ship position and rotation in a pipeline with raycasts 0 Answers

[Unity3d 5.1] Multiplayer - Multiple match - dedicated server 1 Answer

a function is working on editor but not working on android divice 1 Answer

Is Build and Run essential to make a Gear VR app ? 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