Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
1
Question by Kongam03 · Jul 16, 2021 at 07:57 AM · boxcollider2dlines

How to find the 2D box collider center line (xy)?

I want the center of the four sensors to detect only the center of the collider of the lines. Sometimes the player goes off the line.

The sensor's code: alt text

 void OnTriggerStay2D (Collider2D coll2D)
 {
     if (coll2D.gameObject.name == "LinesX") {
         LineRight = true;
         print ("Right = true");
     }
 }

 void OnTriggerExit2D (Collider2D coll2D)
 {
     if (coll2D.gameObject.name == "LinesY") {
         LineRight = true;
     } else {
         LineRight = false;
         print ("Right = false");
     }
 }     

}

image2.png (108.2 kB)
Comment
Add comment · Show 8
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 rage_co · Jul 16, 2021 at 08:20 AM 0
Share

I don't know exactly but because the object's origin is situated in the middle of it......and the box collider is applied uniformly on it....maybe you can use the transform values to achieve the intended effect

avatar image Anonymous620 · Jul 16, 2021 at 11:51 PM 0
Share

Like @rage_co, you could get the axis of the line, For example:


X(Vertical) and Y(Horizontal)


and when you detect a collision, you get the X or Y axis of the line and set your player's X or Y axis to that value

avatar image Kongam03 Anonymous620 · Jul 17, 2021 at 10:40 AM 0
Share

Can you describe these to me in a script?

avatar image rage_co Kongam03 · Jul 17, 2021 at 12:01 PM 0
Share

maybe something along the lines of checking and storing the position of the object every frame and the last frame and if it's less than the x value of the sensor's transform in the last frame and equal to or more than that in the current one....call the desired funtion....same if it's more than that value in the last frame and equal or less than that in the current frame....and the same can be carried onto the y value....might get a lot more complicated by adding a third axis but for a 2d game like yours, this should work just fine...and should not present problems like missing the collision too

Show more comments
Show more comments

1 Reply

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

Answer by Anonymous620 · Jul 18, 2021 at 04:48 AM

@Kongam03 I have added onto the collision script I gave you in your last post: https://answers.unity.com/questions/1847116/sliding-movement-script-in-2d-doesnt-stop.html?childToView=1847642#comment-1847642 this script basically only lets it change lines if the player's transform is in a range between the centre of the line and then it sets the transform to the centre of the line. because the range between the centre of the line is so small, you don't notice the player's transform being set. keep in mind though that this system does stop the player moving in the intersection of 2 lines if they are pressing a left or right key and a up or down key at the same time. I hope you didn't write too much of your script @rage_co.

  PlayerScript playerScript;
         GameObject player;
         float difference;
         // Start is called before the first frame update
         void Start()
         {
             player = GameObject.Find("Player");
             playerScript = player.GetComponent<PlayerScript>();
             difference = 0.05f;
         }
     
         // Update is called once per frame
         private void OnTriggerStay2D(Collider2D collision)
         {
             print(collision.gameObject.name);
             if (player.transform.position.x <= collision.transform.position.x + difference && player.transform.position.x >= collision.transform.position.x - difference)
             {
                 print("X is equal");
             }
             if(player.transform.position.x <= collision.transform.position.x + difference && player.transform.position.x >= collision.transform.position.x - difference && gameObject.name == "Top")
             {
                 playerScript.top = true;
                 if (Input.GetKey(KeyCode.UpArrow))
                     player.transform.position = new Vector2(collision.transform.position.x, player.transform.position.y);
             }
             if (player.transform.position.x <= collision.transform.position.x + difference && player.transform.position.x >= collision.transform.position.x - difference && gameObject.name == "Bottom")
             {
                 playerScript.bottom = true;
                 if (Input.GetKey(KeyCode.DownArrow))
                     player.transform.position = new Vector2(collision.transform.position.x, player.transform.position.y);
             }
             if (player.transform.position.y <= collision.transform.position.y + difference && player.transform.position.y >= collision.transform.position.y - difference && gameObject.name == "Left")
             {
                 playerScript.left = true;
                 if (Input.GetKey(KeyCode.LeftArrow))
                     player.transform.position = new Vector2(player.transform.position.x, collision.transform.position.y);
             }
             if (player.transform.position.y <= collision.transform.position.y + difference && player.transform.position.y >= collision.transform.position.y - difference && gameObject.name == "Right")
             {
                 playerScript.right = true;
                 if(Input.GetKey(KeyCode.RightArrow))
                     player.transform.position = new Vector2(player.transform.position.x, collision.transform.position.y);
             }
         }
         private void OnTriggerExit2D(Collider2D collision)
         {
             if (gameObject.name == "Top")
             {
                 playerScript.top = false;
             }
             if (gameObject.name == "Bottom")
             {
                 playerScript.bottom = false;
             }
             if (gameObject.name == "Left")
             {
                 playerScript.left = false;
             }
             if (gameObject.name == "Right")
             {
                 playerScript.right = false;
             }
         }

I'm sorry if you've changed the script at all but i thought it would be easier to solve if i just changed something i had already created. Hope this helps.

Comment
Add comment · Show 4 · 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 rage_co · Jul 18, 2021 at 07:53 AM 1
Share

hmmm...i did not understand what the question actually was.....but i suppose my method was too complicated since i had thought of it as something for free movement (it was a bit unnecessary for even that)......so your script is perfect i think.....and don't worry...i didn't get much time to make anything today so i hadn't even started

avatar image Anonymous620 rage_co · Jul 18, 2021 at 09:53 AM 1
Share

I'm glad you didn't waste time on it. It was much easier for me as I already had the scene and script from his last post setup and experienced the same problem. I can see why you didn't understand the question. Also I have all day because it's school holidays.

avatar image Kongam03 · Jul 21, 2021 at 10:40 AM 1
Share

Thank you for your help!

avatar image rage_co Kongam03 · Jul 21, 2021 at 12:39 PM 1
Share

if that helped you, please accept the answer and help lessen the load on the site, plus this will make it easy for others to find an answer

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

122 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

Related Questions

inconsistent lines errors?! 2 Answers

Don't delete discrete lines in Vectrosity 2 Answers

tall lines when Importing a Heightmap 0 Answers

Black lines on the edge of cubes 3 Answers

How to add and modify multiple boxcollider2Ds through script? 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