winstxnhdw / lc-hax

A powerful, feature-rich and highly performant internal cheat for the co-op indie horror video game, Lethal Company.
78 stars 25 forks source link

[Feature Request]: Made New Possession Controls #312

Closed D1GQ closed 5 months ago

D1GQ commented 5 months ago

Decided to make my own code for the RigidbodyMovement.cs to use more natural player movement, and everything works splendidly but there are an few issues that I have not been able to fix, I think both revolve around the PossessionMod.cs file.

Issues

I'm not the best at coding so this code is probably pretty wonky but if you want you can edit off of it.

using UnityEngine;
using GameNetcodeStuff;
using UnityEngine.InputSystem;
using System.Collections.Generic;

namespace Hax {
    public class RigidbodyMovement : MonoBehaviour {
        const float baseSpeed = 5.0f;
        const float sprintSpeedMultiplier = 2.8f; // Multiplier for sprinting speed
        const float controlSlowdoneMultiplier = 0.5f; // Slowdown multiplier when left control is held
        const float sprintDuration = 0.0f; // Duration sprint key must be held for sprinting (adjust as needed)
        const float jumpForce = 10.0f;
        const float gravity = 25.0f;

        CharacterController characterController;
        Vector3 velocity = Vector3.zero;
        bool isSprinting = false;
        bool isSprintHeld = false;
        float sprintTimer = 0f;
        Keyboard keyboard;

        public RigidbodyMovement() {
            characterController = GetComponent<CharacterController>();
            keyboard = Keyboard.current;
        }

        // Define the desired width, height, and depth of the collision box
        const float adjustedWidth = 0.0f; // Adjust as needed
        const float adjustedHeight = 0.0f; // Adjust as needed
        const float adjustedDepth = -0.5f; // Adjust as needed

        void Awake() {
            characterController = gameObject.AddComponent<CharacterController>();

            // Adjust height and center here if necessary
            transform.localScale = new Vector3(adjustedWidth, transform.localScale.y, adjustedDepth);
            characterController.height = 0.0f; // Adjust as needed
            characterController.center = new Vector3(0f, 0.3f, 0.5f); // Adjust as needed

            keyboard = Keyboard.current;
        }

        public void Init() {
            if (Helper.LocalPlayer is not PlayerControllerB localPlayer) return;
            this.gameObject.layer = localPlayer.gameObject.layer;
        }

        void Update() {
            Vector2 moveInput = new Vector2(keyboard.dKey.ReadValue() - keyboard.aKey.ReadValue(),
                                            keyboard.wKey.ReadValue() - keyboard.sKey.ReadValue()).normalized;

            // Apply speed modifier based on left control key
            float speedModifier = 1.0f;
            if (keyboard.leftCtrlKey.isPressed) {
                speedModifier = controlSlowdoneMultiplier;
            }

            // Calculate movement direction relative to character's forward direction
            Vector3 forward = Vector3.ProjectOnPlane(transform.forward, Vector3.up).normalized;
            Vector3 right = Vector3.ProjectOnPlane(transform.right, Vector3.up).normalized;
            Vector3 moveDirection = forward * moveInput.y + right * moveInput.x;

            // Remove vertical component from the movement direction
            moveDirection.y = 0f;

            moveDirection *= isSprinting ? baseSpeed * sprintSpeedMultiplier * speedModifier : baseSpeed * speedModifier;

            ApplyGravity();

            // Attempt to move
            characterController.Move(moveDirection * Time.deltaTime);

            // Jump if jump key is pressed
            if (keyboard.spaceKey.wasPressedThisFrame) {
                Jump();
            }

            // Sprinting mechanic: Hold to sprint
            if (keyboard.leftShiftKey.isPressed) {
                if (!isSprintHeld) {
                    sprintTimer = 0f;
                    isSprintHeld = true;
                }

                if (!isSprinting && sprintTimer >= sprintDuration) {
                    isSprinting = true;
                }

                sprintTimer += Time.deltaTime;
            }
            else {
                isSprintHeld = false;

                if (isSprinting) {
                    isSprinting = false;
                }
            }
        }

        void ApplyGravity() {
            velocity.y -= gravity * Time.deltaTime;
            characterController.Move(velocity * Time.deltaTime);
        }

        void Jump() {
            velocity.y = jumpForce;
        }
    }
}
winstxnhdw commented 5 months ago

Thanks for the work!

You’ll just have to disable gravity when noclip is enabled. Glitching into void is because during a possession, we force a small sphere into the enemy. Sometimes, this clips into the ground, which then exerts a tremendous force back to the sphere. I am not sure what’s the best solution for this either.

D1GQ commented 5 months ago

Thanks for the work!

You’ll just have to disable gravity when noclip is enabled. Glitching into void is because during a possession, we force a small sphere into the enemy. Sometimes, this clips into the ground, which then exerts a tremendous force back to the sphere. I am not sure what’s the best solution for this either.

Also found another issue I updated the comment, and np.