Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 GameMuncherz · Aug 13, 2012 at 09:13 AM · physicsraycastgravity

false gravity on a cube planet

How do you change your characters gravity and rotation so that whatever face of the cube your on, your feet point to it and the gravity pulls you toward the face. I have somewhat of an idea but I'm having trouble with the code. heres my idea:

alt text

untitled-1.jpg (55.7 kB)
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

6 Replies

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

Answer by mikebelotti · Aug 13, 2012 at 12:55 PM

You only need one raycast. Cast from your character's position to the center of the cube. hit.normal will be the surface normal of the side of the cube your character is closest to (and, therefore, which side is pulling him). Have your character's up vector become hit.normal, and then pull your character in the direction of -hit.normal. Doing it this way will work for more complex convex objects (like a capsule or cylinder) and ones that rotate too.

Comment
Add comment · Show 8 · 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 GameMuncherz · Aug 13, 2012 at 01:56 PM 0
Share

Will this work for a cube too though because i remember seeing something like this for a sphere. Wouldnt my character start acting all funny when it gets farther away from the center of the cube.

avatar image mikebelotti · Aug 13, 2012 at 02:43 PM 0
Share

Yes, it'll work for a cube. Just make sure to use -hit.normal as your gravity. If you were to ins$$anonymous$$d use, say, the vector from your character to the center of the cube, then you would get funny behavior because the gravity would appear be stronger when your character is on one of the corners of the cube (because, in that case, he is in fact further from the center). So yeah; don't do that.

This might be a useful read: http://www.gamasutra.com/view/feature/3593/games_demystified_super_mario_.php

avatar image fafase · Aug 13, 2012 at 03:03 PM 0
Share

True, only one ray is useful. For my answer, I used 4 rays as I thought of a way to change gravity in a whole world to stick on walls and ceilings...

avatar image GameMuncherz · Aug 14, 2012 at 12:55 AM 0
Share

Idk how to use on normals. Do I use Physics.Raycast to hit the center, like from my position to cube position? and for the gravity do I use AddForce or velocity? Sorry with all these question but this problem is really making my head spin. I've read that article before and I've tried the gravity on a sphere, but cubes seemed to work better in my game. Oh and if i enter a building or stand on a different object, will this mess anything up?

@fafase I liked your idea but my cube is using all 6 sides. $$anonymous$$y character is in 3rd person and can go anywhere on the cube.

avatar image mikebelotti · Aug 14, 2012 at 12:46 PM 0
Share

Yes, you cast from the character's position to the cube's center. And if you put other stuff on the cube's surface, like buildings, just make sure they are on a different layer than the cube. Then, you can pass the cube's layer into Physics.Raycast (the layer$$anonymous$$ask parameter) and the ray will only hit the cube. See the example here: http://docs.unity3d.com/Documentation/Components/Layers.html

Show more comments
avatar image
1

Answer by Tim-Michels · Aug 13, 2012 at 09:18 AM

You could add several triggers which act as forcefield. That way you should disable gravity itsself, but in the OnTriggerStay of the triggers, you could add force in certain directions.

edit

You can also use the OnTriggerEnter and OnTriggerExit to manage the rotation of you character.

Comment
Add comment · Show 1 · 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 GameMuncherz · Aug 13, 2012 at 12:44 PM 0
Share

I was thinking of doing that but i have to change the collider size everytime i exit or enter a collider beacuase if i dont, the edges of the cube will have empty space or intersected colliders. i have 6 colliders by the way. So like function OnTriggerEnter(){ find collider with this tag. change the size change my characters gravity }

avatar image
0

Answer by sam32x · Aug 13, 2012 at 09:26 AM

here's a probably bad idea but it might be good use transform.LookAt to point the character at the cube and check to see if the x, y and z rotations are divisible by 90, if they aren't then they go back to what they were last frame but if they are then they stay like that.

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
avatar image
0

Answer by Griffo · Aug 13, 2012 at 03:14 PM

Have you seen this?

http://forum.unity3d.com/threads/33987-Character-align-to-surface-normal

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
avatar image
0

Answer by malikcgcs · Jul 01, 2013 at 09:45 PM

http://forum.unity3d.com/threads/39618-My-first-sucessful-script!-%28Planet-Gravity-%29

http://answers.unity3d.com/questions/359759/how-to-program-gravity-for-planets.html http://www.gamasutra.com/view/feature/3593/games_demystified_super_mario_.php

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
  • 1
  • 2
  • ›

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

15 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

Related Questions

[C#] Raycast based physics and clipping 0 Answers

Applying Normal force from ground to player 1 Answer

Raycasting problem 2 Answers

Calculate where an object is going to land 0 Answers

Edit this script so that on the second jump change the gravity to 5 2 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