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 Martin251 · Oct 03, 2018 at 06:58 AM · rigidbodygravityrotatearound

Player RotateAround Object. If player jumps, rotation speed decreases. Why?

dear unity community,

I make a game, where the player stands on a planet and automatically walks around it. The player always rotates around the world in the same direction. Always around the vector3.right-axis of the planet. There is also an enemy, who walks on the opposite side of the planet. Like the player, the enemy also rotates around the planet in the same direktion as the player does (see picture 1).

The player can fly away from the planet only in the transform.up-direction and gravity pulls him down to earth.

Here comes my problem: When i jump resp. fly away and apply force to the rigidbody, the rotation movement of my player slightly decreases and the enemy get closer to the player (see picture 2). For my game, it's essential, that the player rotates alway at the same speed around the planet whatever altitude he has. For the rotation around the planet, i use RotateAround. As RotateAround rotates the player by angle degrees around the planet, the altitude should not affect the rotation speed. So i guess, that the applied up-force affect the rotationspeed somehow?

After a lot of web-searching, i am still clueless. I hope, there is some smart programmer out there, who can help me.

Thank you in advance!

Here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class playerMovement : MonoBehaviour
 {
     public float gravity = -10;
     public float jumpSpeed = 15;
     public GameObject planet;
 
     // Use this for initialization
     void Start()
     {
     }
 
     // Update is called once per frame
     void Update()
     {
         // player Rotation
         Vector3 targetDir = (transform.position - planet.transform.position).normalized;
         Vector3 bodyUp = transform.up;
         transform.rotation = Quaternion.FromToRotation(bodyUp, targetDir) * transform.rotation;
 
         // RotateAround planet (Movement)
         transform.RotateAround(planet.transform.position, Vector3.right, 60 * Time.deltaTime); 
     }
 
     private void FixedUpdate()
     {
         // jump
         if (Input.GetButton("Jump"))
         {         
           GetComponent<Rigidbody>().velocity += transform.up * jumpSpeed * Time.deltaTime;   //transform.up   
         }
         // Applying gravity to the controller
         GetComponent<Rigidbody>().velocity += transform.up * gravity * Time.deltaTime;   // transform.up
     }
 }

[2]: /storage/temp/125530-rotatearound1.png

[3]: /storage/temp/125531-rotatearound2.png

rotatearound2.png (64.9 kB)
rotatearound1.png (63.5 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DaoGear · Oct 03, 2018 at 07:29 AM

My English is too bad to understand 100%

If I'm not wrong you want the rotation speed of the player in jump equal with the rotation speed when he is on the ground?

   public Transform target;
   public float rotationSpeed = 1f;
   public bool rotationConstantSpeed;
   float mDistanceCache;
 
 
     // Use this for initialization
     void Start () {
     mDistanceCache = (target.position - transform.position).magnitude;
     }
     
     // Update is called once per frame
     void Update () {
     float newSpeed = rotationSpeed;
     if (rotationConstantSpeed) {
       float newDistance = (target.position - transform.position).magnitude;
       float ratio = newDistance / mDistanceCache;
       newSpeed = rotationSpeed * ratio;
     }
     transform.RotateAround(target.position, Vector3.up, newSpeed * Time.deltaTime);
  }
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 Martin251 · Oct 03, 2018 at 08:06 AM

Hello DaoInc

Thank you very much for your fast reply. Yes, i want the rotation speed of the player when jumping equal with the rotation speed when he is on the ground. Or maybe a bit clearer: If the player is on the ground or jumping, he need for one full rotation around the planet the same amount of time. Yesterday, i tried the same code, as you gave me. Except for the distance calculations, i used Vector3.Distance. When i apply your code, the player is rotating too fast, when in the air. As i wrote, RotateAround uses angle degrees for the movement calculation. Using RotateAround, the altitude should not affect the time, the player need to rotate around the planet. When i disable gravity, pause the game and drag the player manualy up in the air, the player rotates around the planet as i want (he need the same amount of time to rotate around the planet as the enemy does). So i guess, the speed decrease has to do with the jump itself? Many thanks for your help.

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

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

Related Questions

Gravity.cs help? 3 Answers

Artificial Gravity 2 Answers

Custom gravity based on normal vector and rigidbody.addForce 2 Answers

Sphere + rigidbody + character controller 1 Answer

Applying force to a rigidbody 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