Rotation Question

I am creating a 3d platformer where there is arbitrary gravity. As such the player jumps from planet to planet and orients towards the gravity…at least that is my intention.

My current code below takes the players movement input vector and applies rotation based on the input value. How can I add the gravity Vector3 to the code below?

Vector3 Gravity = GravitySource.GetGravity();

Vector3 inputDirection = Vector3.Normalize(new Vector3(inputSource.GetRawMove.x, 0.0f, inputSource.GetRawMove.y));

            float _targetRotation = Mathf.Atan2(inputDirection.x, inputDirection.z) * Mathf.Rad2Deg + playerInputSpace.transform.eulerAngles.y;
            float rotation = Mathf.SmoothDampAngle(transform.eulerAngles.y, _targetRotation, ref rotationSpeed, RotationSmoothTime);
           
            transform.rotation = Quaternion.Euler(0f, rotation, 0f);

Well you have many ways of doing that. But one would be to store your player’s velocity in a Vector3, for instance: private Vector3 velocity = new Vector3(0, 0, 0);. And each frame, you would take the gravity into account:

velocity += Gravity * Time.deltaTime;

depending on what you want to do with your input, you might add:

velocity += inputDirection * Time.deltaTime;

and then you movement code would look something like:

Vector3 playerPosition = transform.position;
playerPosition += velocity * Time.deltaTime;
transform.position = playerPosition;

Be careful though: with this code, your player will accelerate without any restriction, and might end up being very fast.

1 Like