• 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 Dreeka · Jul 30, 2011 at 05:41 PM · rotationgravitycalculate

Rotating for gravity.

Hello,

I have created a pickup that allows the player to change the direction of the gravity (can change to x, y, z axis).

However, to be able to fully move in the modified gravity direction, I would need to rotate the player. The player object has 3 c$$anonymous$$lds, the marble, the button collider, and the main camera.

I have tried with transform.rotate, however, it isn't rotating the viewport of the camera. only the other 2 object. How shoul I do it?

Also, because you can change the gravity to 6 directions (x, -x, y, -y, z, -z), how should I calculate how much rotation is needed for each variation (somekind of a script that do tha calculation, because programming all the variation manually may be not the best solution)?

Thanks, Akos

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 aldonaletto · Jul 30, 2011 at 07:09 PM

If the camera is a player's c$$anonymous$$ld, it should rotate or move with the player, no doubt about t$$anonymous$$s. It would not do it only if some script attached to the camera was controlling its orientation (a c$$anonymous$$ld always follows the parent movement and rotation, but the parent isn't affected by the c$$anonymous$$ld).
The best way to set the player orientation according to gravity is to set its transform.up vector to the negative of the gravity, like t$$anonymous$$s:

  transform.up = -myGravity.normalized;
where transform is the player's transform, of course, and myGravity is a vector pointing to the down direction in your system (like Physics.gravity in a regular system).

EDITED2: T$$anonymous$$s version of MouseOrbit keeps the parent object orientation, but follows the target position. Save t$$anonymous$$s with a different name (MouseOrbitC$$anonymous$$ld.js, for instance) and assign it to the camera (remove the original MouseOrbit).

// follow the target with mouse X and Y control // but keeping the parent object orientation var target: Transform; var distance = 10.0; // distance from target var xSpeed = 250.0; var ySpeed = 120.0; var yMinLimit = -20; var yMaxLimit = 80;

private var x = 0.0; private var y = 0.0;

function Start () { var angles = transform.localEulerAngles; x = angles.y; y = angles.x; // Make the rigid body not change rotation if (rigidbody) rigidbody.freezeRotation = true; }

function LateUpdate () { x += Input.GetAxis("Mouse X") xSpeed 0.02; y -= Input.GetAxis("Mouse Y") ySpeed 0.02; y = ClampAngle(y, yMinLimit, yMaxLimit); var rotation = Quaternion.Euler(y, x, 0); transform.localRotation = rotation; if (target){ var position = rotation * Vector3(0.0, 0.0, -distance) + target.position; transform.position = position; } }

static function ClampAngle (angle : float, min : float, max : float) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp (angle, min, max); }

EDITED3: One more version! T$$anonymous$$s time the script makes the camera follow the target object, but with orientation based on Physics.gravity. In theory, it doesn't matter if the camera is c$$anonymous$$lded to some object or not, so you can remove the camera from the parent object if you want.

var target: Transform; var distance = 10.0; // distance from target var xSpeed = 250.0; var ySpeed = 120.0; var yMinLimit = 0; var yMaxLimit = 60;

private var x = 0.0; private var y = 0.0;

function Start () { // Make the camera rigidbody (if any) ignore physics rotation if (rigidbody) rigidbody.freezeRotation = true; }

function LateUpdate () { x += Input.GetAxis("Mouse X") xSpeed 0.02; y -= Input.GetAxis("Mouse Y") ySpeed 0.02; y = Mathf.Clamp(y, yMinLimit, yMaxLimit); // apply gravity orientation var rotation = Quaternion.FromToRotation(Vector3.up, -Physics.gravity); rotation = Quaternion.Euler(y, x, 0); // apply mouse control transform.rotation = rotation; // set camera orientation if (target){ var position = rotation Vector3(0,1,-distance) + target.position; transform.position = position; } }

Comment
Add comment · Show 14 · 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 Dreeka · Jul 30, 2011 at 07:24 PM 0
Share

The camera has a Mouse Orbit script attached to it. I have tried your idea with the follwoing way: myGravity = Physics.gravity.down; transform.up = -myGravity.normalized; However, The camera is still not rotating.

avatar image aldonaletto · Jul 30, 2011 at 07:54 PM 0
Share

I bet this MouseOrbit script is sabotaging you: the player rotates, but MouseOrbit keeps the camera at the original orientation. I'll check this script to know what can be done.

avatar image Dreeka · Jul 30, 2011 at 08:08 PM 0
Share

Thanks :)

avatar image aldonaletto · Jul 30, 2011 at 08:36 PM 0
Share

There goes a new camera script - I edited the answer to include a local version of MouseOrbit.

avatar image Dreeka · Jul 31, 2011 at 08:49 AM 0
Share

Thank you for your help so far, however I think I have made a mistake in my question. The player object is an empty game object, with 3 child (the main camera, the marble, and the marbleButtonCollider). For your script, I would need to make the main camera the child of the marble. However, I have an issue with that. If I try it, the camera also rotates with the marble. Is there any way to fix that? Also, I have applied the myGravity = Physics.gravity.down; transform.up = -myGravity.normalized to the empty game object player. I hope you can help me with this one aswell :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rigidbody unaffected by standard unity gravity and script gravity 3 Answers

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

Sphere rotation based on object on the sphere 0 Answers

switching direction of gravity 1 Answer

make an object "Look at" an other object 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