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 /
  • Help Room /
This question was closed Apr 25, 2018 at 11:17 AM by tormentoarmagedoom for the following reason:

Too subjective and argumentative

avatar image
0
Question by jmoocow2003 · Apr 23, 2018 at 01:54 AM · rotationbugontriggerstayunknown

Rotation stops OnTriggerStay2D

So, I have a character that turns to the right or left depending on where the mouse is on the screen, and when they're turned right, OnTriggerStay2D works for a second and then stops. I have no idea what is causing this. Here is my code for the player and the OnTriggerEnter2D script:

region variables

public float jumpHeight = 5; public float moveSpeed; public float maxSpeed; public Rigidbody2D rb; #endregion variables

 void Update () {

     //float horizontal = Input.GetAxis("Horizontal") * moveSpeed;

     //rb.AddForce(transform.right * horizontal);

     if (Input.GetKeyDown(KeyCode.Space) && rb.velocity.y == 0)
     {
         rb.AddForce(transform.up * jumpHeight);
     }


     if (rb.velocity.magnitude > maxSpeed)
     {
         rb.velocity = rb.velocity.normalized * maxSpeed;
     }

     Vector2 mouse = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);

     if (mouse.x < Screen.width / 2)
     {
         transform.rotation = Quaternion.Euler(0, 180, 0);

         #region movement
         if (Input.GetKey("d"))
         {
             rb.AddForce(-transform.right * moveSpeed);
         }

         if (Input.GetKey("a"))
         {
             rb.AddForce(transform.right * moveSpeed);
         }

         if (Input.GetKeyUp("d"))
         {
             rb.velocity = Vector2.zero;
         }

         if (Input.GetKeyUp("a"))
         {
             rb.velocity = Vector2.zero;
         }
         #endregion movement
     }

     if (mouse.x > Screen.width / 2)
     {
         transform.rotation = Quaternion.Euler(0, 0, 0);

         #region movement
         if (Input.GetKey("d"))
         {
             rb.AddForce(transform.right * moveSpeed);
         }

         if (Input.GetKey("a"))
         {
             rb.AddForce(-transform.right * moveSpeed);
         }

         if (Input.GetKeyUp("d"))
         {
             rb.velocity = Vector2.zero;
         }

         if (Input.GetKeyUp("a"))
         {
             rb.velocity = Vector2.zero;
         }
         #endregion movement
     }

public float speed = 10; public Transform player; public Transform elevator; public Transform starship; public int floorLevel = 4;

 void OnTriggerStay2D (Collider2D c)
 {

     Debug.Log("testing testing 123 can you hear me let me see");

     if (c.tag == "Player")
     {
         player.parent = elevator;

         #region down
         if (Input.GetKeyDown("s") && floorLevel == 1)
         {
             Debug.Log("You can't go down any further!");
         }
         if (Input.GetKeyDown("s") && floorLevel == 2)
         {
             floorLevel = 1;
         }
         if (Input.GetKeyDown("s") && floorLevel == 3)
         {
             floorLevel = 2;
         }
         if (Input.GetKeyDown("s") && floorLevel == 4)
         {
             floorLevel = 3;
         }
         #endregion down

         #region up
         if (Input.GetKeyDown("w") && floorLevel == 4)
         {
             Debug.Log("You can't go up anymore!");
         }
         if (Input.GetKeyDown("w") && floorLevel == 3)
         {
             floorLevel = 4;
         }
         if (Input.GetKeyDown("w") && floorLevel == 2)
         {
             floorLevel = 3;
         }
         if (Input.GetKeyDown("w") && floorLevel == 1)
         {
             floorLevel = 2;
         }
         #endregion up
     }
 }

 void OnTriggerExit2D ()
 {
     player.parent = starship;
 }
Comment
Add comment · Show 2
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 tormentoarmagedoom · Apr 23, 2018 at 08:20 AM 0
Share

Good day,.

How you detect the trigger collision? Do you have a collider in your player?. $$anonymous$$ake sure that when you rotate, the collider is not rotating with a distant pivot point, so is going outside the area of collision

avatar image jmoocow2003 tormentoarmagedoom · Apr 23, 2018 at 09:18 PM 0
Share

Hi, First, I detect trigger collision using collides and rigid bodies (all 2D). Second, I rotated the character at their y axis, only flipping it to 180 and 0 depending on where the mouse is. And third, I fixed the problem by having my character rotate 360 degrees rather than 0 degrees. I'm not sure how that fixed it, but it did. It seems it only works past 180 degrees, any explanation would be greatly appreciated.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by jmoocow2003 · Apr 23, 2018 at 09:20 PM

Fixed it by making the character rotate to 360 degrees rather than to 0 degrees, because the OnTriggerStay2D seems to only work when your sprite is rotated past 180 degrees. I have no idea why this works, but it did, and it produces the same effect. Any explanation would be greatly appreciated, thanks.

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

Follow this Question

Answers Answers and Comments

216 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 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

Quaternion.FromToRotation - is it a bug? Possible workarounds? 1 Answer

When exporting from Blender, animation gets messed up 0 Answers

Very basic rotation BUG. transform.up = transform.up sets Y rotation to 0. 1 Answer

2d Animation, Rotating GameObject bug 1 Answer

Trouble with rotating player when gravity is shifted 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