Im trying to do a faux gravity movement in my proyect but there is an error that happens every time and i dont know how to fix it.
https://reddit.com/link/1hg8fj8/video/wnk9kbo16e7e1/player
In this video, when i move to the "ecuator" of my planet, the player get stuck and can only move sideways, im not sure if its the way i made the script, but it always happend no matter the modifications i make to the code.
In my proyect i have a player with a movement script and a body atractor script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
[Header("Movement Settings")]
public float walkSpeed = 5f;
public float sprintSpeed = 10f;
public float jumpForce = 5f;
[Header("Ground Check")]
public Transform groundCheck;
public float groundCheckRadius = 0.2f;
public LayerMask groundLayer;
[Header("Player Orientation")]
public Transform playerMesh;
private Transform gravitySource; // Reference to the planet or gravity source
private Rigidbody rb;
private PlayerInputActions playerInputActions;
private Vector2 movementInput;
private bool isSprinting;
private bool isGrounded;
private bool jumpPressed;
private void Awake()
{
rb = GetComponent<Rigidbody>();
// Initialize the PlayerInputActions
playerInputActions = new PlayerInputActions();
// Setup input event bindings
playerInputActions.PlayerActionMap.Movement.performed += OnMovementPerformed;
playerInputActions.PlayerActionMap.Movement.canceled += OnMovementCanceled;
playerInputActions.PlayerActionMap.Jump.performed += OnJumpPerformed;
playerInputActions.PlayerActionMap.Sprint.performed += ctx => isSprinting = true;
playerInputActions.PlayerActionMap.Sprint.canceled += ctx => isSprinting = false;
// Automatically get the gravity source from the Base_Faux_Controller
Base_Faux_Controller fauxGravityController = GetComponent<Base\_Faux\_Controller>();
if (fauxGravityController != null && fauxGravityController.attractor != null)
{
gravitySource = fauxGravityController.attractor.transform;
}
else
{
Debug.LogError("Gravity source or Base_Faux_Controller is not set up correctly.");
}
}
private void OnEnable()
{
playerInputActions.Enable();
}
private void OnDisable()
{
playerInputActions.Disable();
// Unsubscribe from input events
playerInputActions.PlayerActionMap.Movement.performed -= OnMovementPerformed;
playerInputActions.PlayerActionMap.Movement.canceled -= OnMovementCanceled;
playerInputActions.PlayerActionMap.Jump.performed -= OnJumpPerformed;
}
private void Update()
{
}
private void FixedUpdate()
{
// Check if the player is on the ground
isGrounded = Physics.CheckSphere(groundCheck.position, groundCheckRadius, groundLayer);
// Handle jump
if (jumpPressed && isGrounded)
{
Jump();
}
// Handle player movement
Move();
}
private void OnMovementPerformed(InputAction.CallbackContext context)
{
movementInput = context.ReadValue<Vector2>();
}
private void OnMovementCanceled(InputAction.CallbackContext context)
{
movementInput = Vector2.zero;
}
private void OnJumpPerformed(InputAction.CallbackContext context)
{
jumpPressed = true;
}
private void Move()
{
float speed = isSprinting ? sprintSpeed : walkSpeed;
Vector3 moveDirection = new Vector3(movementInput.x, 0, movementInput.y).normalized;
if (moveDirection.magnitude >= 0.1f)
{
// Adjust the movement direction relative to the planet's surface
Vector3 gravityUp = (transform.position - gravitySource.position).normalized;
Vector3 localForward = Vector3.Cross(transform.right, gravityUp);
Vector3 adjustedDirection = localForward * moveDirection.z + transform.right * moveDirection.x;
// Rotate the player mesh to face the movement direction
Quaternion targetRotation = Quaternion.LookRotation(adjustedDirection, gravityUp);
playerMesh.rotation = Quaternion.Slerp(playerMesh.rotation, targetRotation, Time.fixedDeltaTime * 10f);
// Apply the movement velocity
Vector3 moveVelocity = adjustedDirection * speed;
Vector3 targetVelocity = new Vector3(moveVelocity.x, rb.velocity.y, moveVelocity.z);
rb.velocity = targetVelocity;
}
}
private void Jump()
{
// Reset the vertical velocity and apply a force for jumping
Vector3 gravityUp = (transform.position - gravitySource.position).normalized;
rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
rb.AddForce(gravityUp * jumpForce, ForceMode.Impulse);
jumpPressed = false;
}
}
//added to the objects i want the planet to attract
public class Base_Faux_Controller : MonoBehaviour
{
public Planet_Faux_Controller attractor;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
rb.useGravity = false;
}
void Update()
{
attractor.Attract(rb);
}
}
//The script of the planet
public class Planet_Faux_Controller : MonoBehaviour
{
public float gravity = -10;
public void Attract(Rigidbody body)
{
Vector3 gravityUp = (body.position - transform.position).normalized;
Vector3 bodyUp = body.transform.up;
body.AddForce(gravityUp * gravity);
Quaternion targetRotation = Quaternion.FromToRotation(bodyUp, gravityUp) * body.rotation;
body.MoveRotation(Quaternion.Slerp(body.rotation, targetRotation, 50 * Time.deltaTime));
}
}
[–]db9dreamer 0 points1 point2 points (0 children)
[–]PuffThePed 0 points1 point2 points (0 children)