- Home /
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
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);
}
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.
Your answer
Follow this Question
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