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
2
Question by Jesus_Freak · Dec 04, 2010 at 03:23 PM · gravityplanet

planetary gravity: falling "off"

now, my player stays within the vicinity of the planet, and when it gets below or above the global y axis (below an altitude of 0), the gravity for the player shifts so that instead of gravity pushing down, it pushes you up.

but you can't move the player at all, so you're stuck shifting between the two gravities, and unable top get back on land....

if there's a better way to make a mario style planet physics, where it looks like the planet rotates, but you the player or gravity itself changes so that the planet is always directly under you no matter where you go on the planet... then couold you give me a hint on how to script that? thanks.

here's my current script;

var speed = 6.0; var jumpSpeed = 8.0; var gravity = 10.0; private var moveDirection = Vector3.zero; private var grounded : boolean = false; var MoveDirection : Vector3;

function Update() { my=moveDirection.y;

 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
 moveDirection = transform.TransformDirection(moveDirection);

 if (grounded) {
     // We are grounded, so recalculate movedirection directly from axes
     moveDirection *= speed;

 } else {
     moveDirection = transform.position.normalized * gravity * Time.deltaTime;

     moveDirection.y=my;     
 }


 if (Input.GetKeyDown ("space")) {
     moveDirection.y = jumpSpeed;
 }
 if(transform.position.y >= 0)
 {
  // Apply gravity
  moveDirection.y -= gravity * Time.deltaTime;
 }
 if(transform.position.y <= 0)
 {
  moveDirection.y += gravity * Time.deltaTime;
 }


 // Move the controller
 var controller : CharacterController = GetComponent(CharacterController);
 var flags = controller.Move(moveDirection * Time.deltaTime);
 grounded = (flags & CollisionFlags.CollidedBelow) != 0;

}

@script RequireComponent(CharacterController)

Comment
Add comment · Show 1
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 Fattie · Sep 15, 2013 at 04:25 PM 0
Share

http://answers.unity3d.com/questions/529536/how-to-make-a-ship-and-its-projectiles-orbit-a-pla.html#comment-537487

3 Replies

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

Answer by GlitchEnzo2 · Dec 04, 2010 at 05:36 PM

A much better way of doing it is to calculate the actual vector between the player and the planet and use that as the gravity vector.

If your planet is positioned at the origin, then all you have to do is normalize the player position:

moveDirection = transform.position.normalized * gravity * Time.deltaTime;

If it's not at the origin, you just have an additional subtraction:

moveDirection = (transform.position - planetCenter).normalized * gravity * Time.deltaTime;

Update - To help explain, here is a simple example:

1) Create a sphere at (0, 0, 0)

2) Scale the sphere to (10, 10, 10)

3) Create a cube at (0, 0, -15) and give it a RigidBody component

4) Disable the gravity on the cube.

5) Apply the following script to the cube

using UnityEngine; public class SphericalGravity : MonoBehaviour { float gravity = -9.8f;

 void Update () 
 {
     rigidbody.velocity += transform.position.normalized * gravity * Time.deltaTime;
 }

}

When you run the game, you should see the cube fall toward the sphere and then land on its surface.

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 Jesus_Freak · Dec 04, 2010 at 11:31 PM 0
Share

thanks! that's perfect... 1-up!

avatar image Jesus_Freak · Dec 05, 2010 at 12:53 AM 0
Share

actually, i tried it, and it's still not working.... i'm guessing we're working with my FPFlyer script since that's the script that controls my movement, so i'll post my updated progress....

avatar image GlitchEnzo2 · Dec 05, 2010 at 01:56 AM 1
Share

Could you elaborate a bit on how it's not working? $$anonymous$$aybe then I could help a little more.

avatar image Jesus_Freak · Dec 05, 2010 at 02:11 AM 0
Share

i'm not sure what you gave me is supposed to do, but i think i know what i need: some code that makes the center of gravity (the point everything goes toward) Vector3.zero, or the origin, since that's the center of my planet. but i'm not sure how to do this...

avatar image GlitchEnzo2 · Dec 05, 2010 at 03:49 AM 1
Share

I editing my answer to give you a more detailed example. Hopefully it makes things clearer for you.

Show more comments
avatar image
0

Answer by Jesus_Freak · Dec 04, 2010 at 04:38 PM

the correct way to do the equator 'shift' is by the code that says:

if(transform.position.y >= 0)
{ 
 // Apply gravity
 moveDirection.y -= gravity * Time.deltaTime;
}
if(transform.position.y <= 0)
{
 moveDirection.y += gravity * Time.deltaTime;
}

in the FPFlyer script.

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 _Petroz · Dec 05, 2010 at 09:25 AM 0
Share

There is a couple of things wrong with this code. Firstly it is only in one dimension, unless your planet is a plane this code certainly acheive the desired result. Secondly it is constant speed where as gravity is applies acceleration. Change this code to what GlitchEnzo has.

avatar image
0

Answer by greg horn · Dec 19, 2010 at 04:43 PM

I am currently trying to achieve exactly the effect discussed here... I have a first person view character and he should be able to walk on a sphere without falling off, and the ground should always appear below him. I'm new to scripting though and haven't been able to implement GlitchEnzos script yet on my First Person Controller. Can anyone provide a step by step walkthrough on how to do this?

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 Muzz5 · Jun 04, 2011 at 07:18 PM 0
Share

Should be a comment!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Planet gravity help need guidance no code 1 Answer

walking around a spheroid planet centered at the origin 1 Answer

Why do the characters jitter at some places? 0 Answers

How to rotate a player when using Mario Galaxy style gravity? 5 Answers

Unity 2D Cube Planet Gravity 0 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