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 dre38w · Aug 23, 2011 at 11:08 AM · charactercontrollergravity

Shifting gravity from the ground to the wall.

First off I'm not wanting anyone to actually make the code for me. Just a nudge in the right direction is what I'm hoping for.

Now for the actual question. I have a cube serving as the "world" and what I'm trying to do is get the player to be able to jump or move to the edge of the cube and then have its gravity shift 90 degrees to be on the new surface of the cube. Another example could be jumping from the floor and then ending up on the wall shifting the gravity from the floor to the wall, which now serves as the floor. Very similar to some moments in Psychonauts.

Now I'm trying to figure how to go about this. Which method I should be using. Shifting the entire player's gravity orientation? Then I'd need to do something else with the player's positional orientation. Or rotate the entire world? Rotating the world seems way too much. I'm using a character controller by the way. But I'm not really sure where I should start.

If anyone knows how to go about starting this suggestions will be very appreciated. Thank you.

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

Answer by Waz · Aug 23, 2011 at 12:09 PM

You cannot use built-in gravity, it is Y only. However, built-in gravity is just a simple constant force, so you can make your own with ConstantForce.

You'll want to rotate the controller, and be very careful with your handling of direction - most existing controller scripts have simplistic world cordite space logic the will not work in your case.

Also be careful with the various Look functions that take a default Up vector of +Y, as you probably want something else.

You could also rotate the world, but that could be costly if it's many static colliders.

Comment
Add comment · Show 5 · 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 dre38w · Aug 24, 2011 at 12:10 AM 0
Share

So I rotate the player and then the character controller separate to meet up with the player's new orientation yes? I got that to work more or less just now I have to figure how to switch the gravity direction relative to the world so that it doesn't go wacky on me. I'm not using a rigidbody by the way. I hard coded everything in. Thanks for the reply.

avatar image dre38w · Aug 25, 2011 at 05:27 PM 0
Share

Found out I can't rotate a character controller because its Y axis is always Y so walking on walls type of deal is going to have to be looked at in a completely different way somehow. Thank you though.

avatar image dre38w · Aug 28, 2011 at 11:40 PM 0
Share

Had to end up using a Rigidbody. $$anonymous$$ore or less it works the same. But at least it allows me to shift gravity from the floor to the wall. Just have to work with the camera and a few other things to compensate for the new orientation but at least I'm making progress. Should I post what I've come up with to make it rotate as an answer or no?

avatar image cico · Sep 30, 2011 at 09:25 AM 0
Share

I would be interested in checking out your solution :)

avatar image Kacer · Sep 30, 2011 at 10:59 AM 0
Share

$$anonymous$$e as well :)

avatar image
0

Answer by dre38w · Oct 04, 2011 at 01:19 AM

Okay here is the gravity shifting part. The actual controls and camera situation is still a work in progress so it's not fully functional.

On Player.

     if (!RotateTriggers.canRotate && !isOnWall)
     {
         rigidbody.AddForce(new Vector3(0, -gravity * rigidbody.mass, 0));

     }
     if (RotateTriggers.canRotate)
     {    
         Quaternion localRot = Quaternion.FromToRotation(-transform.up, Vector3.right) * transform.rotation;
             
         transform.rotation = Quaternion.RotateTowards(transform.rotation, localRot, 10);
             
         rigidbody.AddForce(new Vector3(gravity * rigidbody.mass, 0, 0));
     }
     
     if (RotateTriggersWall.canRotateToWall)
     {
         if (Input.GetButtonDown("Jump"))
             isOnWall = true;
     }
     
     if (isOnWall)
     {
         Quaternion localRot = Quaternion.FromToRotation(-transform.up, Vector3.right) * transform.rotation;
             
         transform.rotation = Quaternion.RotateTowards(transform.rotation, localRot, 10);
             
         rigidbody.AddForce(new Vector3(gravity * rigidbody.mass, 0, 0));
     }
 }

On the trigger that the player would collide with when they fall off a ledge.

 public static bool canRotate = false;
 
 void OnTriggerEnter(Collider other)
 {
     if (other.transform.tag == "Player")
         canRotate = true;
 }

And on the trigger near a wall.

 public static bool canRotateToWall = false;
 
 void OnTriggerEnter(Collider other)
 {
     if (other.transform.tag == "Player")
         canRotateToWall = true;
 }

Keep in mind this is just for one direction. But the other directions should work quite similar. I'm not sure if this is the best or the most efficient way to do it but it works for the gravity shifting portion.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Gravity for my AI 0 Answers

How reliable are isGrounded checks? 1 Answer

Rpg style movement help. 2 Answers

How to add explosion force to player? 1 Answer

disable gravity on Character Controller 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