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 /
avatar image
0
Question by Thevale · Sep 09, 2016 at 05:48 PM · rotationrigidbodygravityoncollisionenteroncollisionexit

Problem with gravity, OnCollisionEnter and OnCollisionExit

Description: I want my gameObject to rotate by 90 degrees when it collides with wall. After that i want to turn off gravity so it can go upwards. When it gets off the wall i want to reset rotation and turn gravaty back on. Problem: When I use OnCollisionEnter it works (rotates gameObject and turns off gravity) but when I add OnCollisionExit (to reset rotation and turn gravity on when there is no more wall) gravity dosen't turn off in OnCollisionEnter function it stays On always. Note that OnCollisionEnter works when OnCollisionExit isn't added in script.

Here is my code

void OnCollisionEnter(Collision col) { if (col.gameObject.tag == "Colli") {

         Vector3 rotatY = new Vector3(-90, 0, 0);
         transform.Rotate(rotatY);
         rb.useGravity = false;
     }

 }
 void OnCollisionExit(Collision other)
 {

     Vector3 resetRot = new Vector3(0, 0, 0);
     transform.Rotate(resetRot);
     rb.useGravity = true;

 }
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

2 Replies

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

Answer by josehzz112 · Sep 10, 2016 at 12:01 AM

Thats because you are not testing if you exited the wall. Change OnCollisionExit() to this:

  void OnCollisionExit(Collision other)
  {
     if(other.gameObject.tag == "Colli")
     {
         Vector3 resetRot = new Vector3(0, 0, 0);
         transform.Rotate(resetRot);
         rb.useGravity = true;
     }
  }
Comment
Add comment · Show 7 · 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 Thevale · Sep 10, 2016 at 11:04 AM 0
Share

Hmm, still not working. Any other idea?

avatar image Thevale · Sep 10, 2016 at 11:25 AM 0
Share

Edit: I just added Debug.Log in OnCollisionEnter and OnCollisionExit and it says that both functions are working. But in reality is not wokring how it should. $$anonymous$$aybe functions are activating to quick or something? $$anonymous$$aybe when OnCollisionEnter rotates gameObject it translates it a bit so OnCollisionExit is automatickly fired so I get illusion that it isn't working? But if that is so it would still need to reset transform.Rotation to original but it dosen't. Any idea?

I hope you guys understand what I mean since english is not my first language. Cheers

avatar image Thevale · Sep 10, 2016 at 11:41 AM 0
Share

I guessed right. It transforms position of my object so OnCollisionExit is automaticly fired after OnCollisionEnter. Thanks @josehzz112 you answered my question and it works, almost. But how do I fix that problem with translation? I'll upvote your answer since it does what I asked first time.

avatar image josehzz112 · Sep 10, 2016 at 01:15 PM 0
Share

I don't quite understand what's your new problem, when you do transform.Rotate() it moves the GameObject? its the object not a square? @Thevale Could you please elaborate more..

avatar image Thevale josehzz112 · Sep 11, 2016 at 12:55 AM 0
Share

So, new problem is that when it collides with wall it rotates gameObject by -90 degrees and it turns off gravity. And when that funcion (OnCollisionEnter) is executed it automaticly executes next funcion (OnCollisionExit) witch rotates object back by 90 degrees and turns gravity on. Becouse of that my game object is always rotating and turning gravity on and off.

What I want from my code is to walk on wall and rotate it by 90 degrees depending on current rotation and wall rotation.

avatar image josehzz112 Thevale · Sep 11, 2016 at 02:38 PM 0
Share

It seens like when the GameObject rotates because of OnCollisionEnter the result rotation makes the GameObject to be outside of the wall so it executes OnCollisionExit.

I don't know how its your set up, but I would consider not turning off gravity when OnCollisionEnter happens.

Also I think this can be ask as a new question, try that, maybe someone else knows how to solve it.

Show more comments
avatar image
0

Answer by akshay_zz · Sep 10, 2016 at 05:04 PM

//May this will work for you void OnCollisionEnter(Collision col) { if (col.gameObject.tag == "Colli") {

      Vector3 rotatY = new Vector3(-90, 0, 0);
      transform.Rotate(rotatY);
      rb.useGravity = false;
  }

else { Vector3 resetRot = new Vector3(0, 0, 0); transform.Rotate(resetRot); rb.useGravity = true; }

}

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

After added a rigidbody in a GameObject, it moves on rotation 2 Answers

Why does the player not move around planet with gravity ? 1 Answer

Why is my Rigidbody freaking out like this? 0 Answers

Need help with collision Detection 0 Answers

Why does my rigidbody move it's transform while rotating? 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