Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
2.9 kB
0
Indexable
Never
// PlaneCreator.cs

using UnityEngine;

namespace MyPlaneMod
{
    public class PlaneCreator
    {
        public bool CreatePlane(Camera playerCamera)
        {
            try
            {
                // Perform raycast to find intersection point
                RaycastHit hit;
                if (Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out hit))
                {
                    // Create plane GameObject
                    GameObject planeObject = GameObject.CreatePrimitive(PrimitiveType.Plane);
                    planeObject.name = "PlayerCreatedPlane";

                    // Set position and scale
                    planeObject.transform.position = hit.point;
                    planeObject.transform.localScale = new Vector3(5f, 1f, 5f);

                    // Rotate to face the player
                    planeObject.transform.LookAt(playerCamera.transform);
                    planeObject.transform.rotation *= Quaternion.Euler(90f, 0f, 0f);

                    // Add MeshCollider
                    planeObject.AddComponent<MeshCollider>();

                    Debug.Log("Plane created successfully!");
                    return true;
                }
                else
                {
                    Debug.LogWarning("No surface found to place the plane.");
                    return false;
                }
            }
            catch (System.Exception ex)
            {
                Debug.LogError($"Error creating plane: {ex.Message}");
                return false;
            }
        }
    }
}

// PlanePatcher.cs

using BepInEx;
using HarmonyLib;
using UnityEngine;

namespace MyPlaneMod
{
    [BepInPlugin("com.mymods.planemod", "Plane Mod", "1.0.0")]
    public class PlanePatcher : BaseUnityPlugin
    {
        private PlaneCreator planeCreator;

        private void Awake()
        {
            planeCreator = new PlaneCreator();
            Harmony.CreateAndPatchAll(typeof(PlanePatcher));
        }

        [HarmonyPatch(typeof(MonoBehaviour), "Update")]
        [HarmonyPostfix]
        static void Update_Postfix()
        {
            if (Input.GetKeyDown(KeyCode.E))
            {
                // Get the player's camera
                Camera playerCamera = Camera.main;
                if (playerCamera != null)
                {
                    // Call CreatePlane from PlaneCreator
                    PlanePatcher instance = BepInEx.Bootstrap.Chainloader.ManagerObject.GetComponent<PlanePatcher>();
                    instance.planeCreator.CreatePlane(playerCamera);
                }
                else
                {
                    Debug.LogError("Player camera not found.");
                }
            }
        }
    }
}
Leave a Comment