Why does the player not move around planet with gravity ?
Hello, I'm learning Unity and tried making a planet that has it's own gravity, I learned the gravity scripts by following a couple of tutorials and made the player movement by tweaking a bit a Third person playerController script I had learned from a Brackeys tutorial to use it on a rigidbody.
The player is a rigidbody and I use cinemachine to for the camera.
The gravity works fine, but I'm currently facing 2 problems that I can't solve.
First the script that manages the gravity and rotates the objects so that their bottom always faces the planet doesn't work on the player when he moves and when the player moves his rotation just goes back to 0,0,0.
Second the player doesn't move in front of him, he moves on the world's x and z axis(if I'm not wrong).
I've left some lines of the different things I've tried to fix this problem so that you may help me understand what I did wrong :
Player Movement (attached to player):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour
{
[SerializeField] private Rigidbody PlayerBody;
[SerializeField] private Transform cam;
[Space]
[SerializeField] private Vector3 playerMovementInput;
[SerializeField] private Vector3 direction;
[Space]
public float speed;
public float jumpForce;
public float turnSmoothTime = 0.1f;
float smoothVelocity;
void Update()
{
float horizontalInput = Input.GetAxisRaw("Horizontal");
float verticalInput = Input.GetAxisRaw("Vertical");
playerMovementInput = new Vector3(horizontalInput, 0f, verticalInput);
direction = new Vector3(playerMovementInput.x, 0f, playerMovementInput.z).normalized;
}
private void FixedUpdate()
{
movePlayer();
}
private void movePlayer()
{
if (direction.magnitude >= 0.1f)
{
// Player rotation based on camera
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg /*+ cam.eulerAngles.y*/;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref smoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
// Movement
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
PlayerBody.MovePosition(PlayerBody.position + transform.TransformDirection(direction) * speed * Time.deltaTime);
//PlayerBody.velocity = new Vector3(moveDir.x, PlayerBody.velocity.y, moveDir.z) * Time.deltaTime;
Debug.Log(targetAngle);
Debug.Log(moveDir);
}
}
}
Gravity (attached to player but should probably work on other objects)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gravity : MonoBehaviour
{
Rigidbody rb;
public float planetGrav;
public Transform planetCenter;
public GameObject currentPlanet;
[Space]
public bool AutoOrient;
public float orientSpeed;
private void Start()
{
rb = GetComponent<Rigidbody>();
planetGrav = currentPlanet.GetComponent<planetSpecs>().gravity;
}
private void FixedUpdate()
{
applyGrav();
}
void applyGrav()
{
Vector3 diff = transform.position - planetCenter.position;
rb.AddForce(-diff.normalized * planetGrav * (rb.mass));
if (AutoOrient)
{
autoOrient(-diff);
}
}
void autoOrient(Vector3 dir)
{
Quaternion orientDir = Quaternion.FromToRotation(-transform.up, dir) * transform.rotation;
transform.rotation = Quaternion.Slerp(transform.rotation, orientDir, orientSpeed * Time.deltaTime);
}
}
Planet information (so that I can make other planets at some point)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class planetSpecs : MonoBehaviour
{
public float gravity = 9.81f;
public bool hasPlayer;
}
Answer by Tarook · Dec 24, 2021 at 01:21 AM
Update : So I've found out that the problems mentioned above were caused by the part of the script that was supposed to make the player rotate to the direction he is walking towards. All I need to do now is find a way to do that without breaking everything which I am unsure of since I rotate the transform on it's y angle based on the direction of the input and y angle of the camera which rotates with the player so I don't get why it rotates around the world's y angle.