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
1
Question by FreelanceFox · Nov 18, 2012 at 10:39 AM · rigidbodycharactervelocitygravity

Modifying Gravity for Local Player

Hello everyone - I'm working on a personal project that involves a character moving around a horizontal cylinder while moving in a set direction. The gravity for the character needs to be constantly pulling them towards the center of the cylinder (which does appear to be working correctly), but when I move the character along their forward vector, I am seeing it 'slide' either farther up (+Y) or down (-Y) the cylinder.

If I remove the forward movement, the character stays oriented correctly and doesn't slide in either direction (which is what I would expect/want). As soon as I do though...well, hence the problem.

The object's rigidbody has all rotational axis locked, and I am not using gravity.

Any insight or help would be appreciated.

Thanks!

Code Snippet:

     //----------------------------------------
     // Class Variables
     //----------------------------------------
     // Private
     private float _RotationSpeedMod = 1f;
     private float _RotationSpeedBase = 120f;
     private float _RotationSpeedMax = 165f;
     private Vector3 _VecGravity = new Vector3();
     private Transform _MyTransform;
     
     // Public
     public float _RotationSpeed = 0;
     public float _Gravity = -9.81f;
     public float _MovementSpeed = 10.0f;
     public float _MaxVelocity = 2.0f;
     
     // Awake
     void Awake()
     {
         _MyTransform = transform;    
     }
 
     // Update is called once per frame
     void Update ()
     {
         // DeltaTime Value
         float DeltaTime = Time.deltaTime;
 
         // Rotate Left
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             transform.RotateAround(Vector3.zero, Vector3.right, DeltaTime * _RotationSpeed );
                 
             _RotationSpeed += _RotationSpeedMod;
             if (_RotationSpeed > _RotationSpeedMax)
             {
                 _RotationSpeed = _RotationSpeedMax;
             }
         }
         // Rotate Right
         else if (Input.GetKey(KeyCode.RightArrow))
         {
             transform.RotateAround(Vector3.zero, Vector3.right, DeltaTime * -_RotationSpeed );
             
             _RotationSpeed += _RotationSpeedMod;
             if (_RotationSpeed > _RotationSpeedMax)
             {
                 _RotationSpeed = _RotationSpeedMax;
             }
         }
         else
         {
             _RotationSpeed = _RotationSpeedBase;
         }
     }
 
     // Physics Update
     void FixedUpdate()
     {
         // DeltaTime Value
         float DeltaTime = Time.deltaTime;
         
         // Compute Gravity Vector
         Vector3 VecUp = _MyTransform.up;
         Vector3 VecFwd = _MyTransform.forward;
         Vector3 VecTemp = new Vector3();
         float GravityRate = _Gravity * DeltaTime;
         float MovementRate = _MovementSpeed * DeltaTime;
         
         Vector3 VecGravity = new Vector3(VecUp.x * GravityRate, VecUp.y * GravityRate, VecUp.z * GravityRate);    
         Vector3 VecMovement = new Vector3(VecFwd.x * MovementRate, VecFwd.y * MovementRate, VecFwd.z * MovementRate);
 
         Debug.Log( "VecGravity: " + VecGravity );
         Debug.Log( "VecMovement: " + VecMovement );
         
         VecTemp = VecMovement + VecGravity;
         //VecTemp = VecGravity;
         
         Debug.Log( "VecTemp: " + VecTemp );
         
         _MyTransform.rigidbody.velocity += VecTemp;
         
         Debug.Log( "Vel: " + _MyTransform.rigidbody.velocity );
             
         // Limiter
         if( _MyTransform.rigidbody.velocity.magnitude > _MaxVelocity )
         {
             VecTemp = _MyTransform.rigidbody.velocity;
             VecTemp.Normalize();
             VecTemp.Set( VecTemp.x * _MaxVelocity, VecTemp.y * _MaxVelocity, VecTemp.z * _MaxVelocity );
             _MyTransform.rigidbody.velocity = VecTemp;
         }
         
         Debug.Log( "Magnitude: " + _MyTransform.rigidbody.velocity.magnitude );        
     }
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 FakeBerenger · Nov 18, 2012 at 10:58 AM

I think the problem is the calculation of your vectors VecUp and Fwd. You're using the player's transform, you should use [closest point on cylinder's axis from the player] -> the player's pos, for the up.

Also, you should use Time.fixedDeltaTime in FixedUpdate.

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 FreelanceFox · Nov 18, 2012 at 07:39 PM 0
Share

Good catch on the Time.fixedDeltaTime!

As for the up vector of the player, it should be set when the player uses the Left or Right keys (and indeed, it does work properly when the player is not moving along the X-axis, only rotating around the cylinder).

$$anonymous$$odifying it to use the normalized directional vector from the cylinder->player's position will also suffice, but as the player slides up/down, it will start to rotate the player out of alignment for what it should be.

I did give your suggestions a go, but still no luck, unfortunately.

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

11 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

Related Questions

How to keep gravity when adding velocity to Rigidbody 3 Answers

Rigidbody y velocity is stuck on 0, gravity is not turned off. 2 Answers

How to set velocity of Rigidbody without changing gravity? 1 Answer

Velocity for movement 0 Answers

G Force Acceleration 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