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
0
Question by ShadowToast · Jul 26, 2017 at 03:58 PM · rotationrigidbodyfauxgravity

Character won't rotate right in faux gravity enviroment.

So I've made a planet (sphere) and dropped a body (capsule) with rigidbody near it. I managed to recreate a gravity effect via simple vector mathematics and it worked! But I'm at the stage where I'm trying to figure out how to move the capsule around the sphere. My first problem that I ran into - is it's rotation. I managed to do it right when I tried making an FPS controller with default gravity (aka on a simple plane) but everything's acting strange with faux gravity. I try to rotate it (the whole capsule with the camera) along the Y axis - it snaps back to it's original rotation, I try to rotate the camera along the X axis - it works but it ignores the clamp. Can someone explain what might be the problem? Here are the scripts I used (both are on the capsule):

Gravity.cs:

 [RequireComponent(typeof(Rigidbody))]
 public class Gravity : MonoBehaviour
 {
     public float gravity = 10.0f;
     private Transform planetT;
 
     private void Awake()
     {
         planetT = GameObject.FindGameObjectWithTag("Planet").GetComponent<Transform>();
     }
 
     private void FixedUpdate()
     {
         Vector3 center = (transform.position - planetT.position).normalized;
         GetComponent<Rigidbody>().AddForce(center * gravity);
         transform.rotation = Quaternion.FromToRotation(Vector3.up, center);
         GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
         GetComponent<Rigidbody>().useGravity = false;
     }
 }

FirstPersonController.cs:

 public class FirstPersonController : MonoBehaviour {

 public float mouseSensitivity = 5.0f;
 Transform cameraT;

 private void Awake()
 {
     cameraT = gameObject.GetComponent<Transform>().GetChild(0).GetComponent<Transform>();
 }

 private void Update()
 {
     float clampedMouseX = Mathf.Clamp(-Input.GetAxis("Mouse Y") * mouseSensitivity, -90, 90);

     transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivity);
     cameraT.localRotation *= Quaternion.Euler(clampedMouseX, 0.0f, 0.0f);
 }

}

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
0

Answer by FortisVenaliter · Jul 26, 2017 at 04:21 PM

You need to use relative rotations. You can't just use Euler when you don't know what the local Y axis is aligned to. Use the Quaternion.AngleAxis with your transform's axes. That should get you in the right direction.

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 ShadowToast · Jul 26, 2017 at 04:51 PM 0
Share

Progress. I redid the code on my FPS controller script like this: cameraT.localRotation *= Quaternion.AngleAxis(clamped$$anonymous$$ouseX, Vector3.right);. I also took off the gravity script and the controller worked fine aside from the fact that it still ignored the clamp (not sure why). So there's something in gravity script that is disallowing any rotation along the Y axis for the capsule and it's not the rigidbody constraint because the issue persisted even when I removed that line.

avatar image FortisVenaliter ShadowToast · Jul 26, 2017 at 05:34 PM 0
Share

Yeah, you're setting the rotation absolutely in the update for the gravity script. That would override any incremental updates.

Also, you need to stop using fixed axes for a spherical body. Ins$$anonymous$$d of Vector3.right, use transform.up, etc.

avatar image ShadowToast FortisVenaliter · Jul 26, 2017 at 06:27 PM 0
Share

Plopped the constraints and useGravity lines in Awake() method - no change. And changing Vector3 to transform don't do much good too...

Show more comments

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

106 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

Related Questions

Engine weight Simulation 2 Answers

Rotating sphere with rigidbody attached 1 Answer

How can I rotate and move a cylindrical rod from one side with respect to other side? 1 Answer

why can't my Rigidbody not rotate on slopes? 1 Answer

Limit Rotation of Rigidbody to 45 Degrees (Handlebars on a Bicycle) 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