Advertisement
Hygcgggnngff

telekinesis antoca

Aug 3rd, 2024
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class Telekinesis : MonoBehaviour
  4. {
  5.     public float range = 10f;                // Range within which objects can be selected
  6.     public float moveSpeed = 10f;            // Speed at which objects are moved
  7.     public LayerMask interactableLayer;      // Layer that defines which objects can be interacted with
  8.  
  9.     private Transform selectedObject;        // Currently selected object
  10.  
  11.     void Update()
  12.     {
  13.         if (Input.GetButtonDown("Fire1"))    // Change to your desired input for selecting an object
  14.         {
  15.             SelectObject();
  16.         }
  17.  
  18.         if (selectedObject != null)
  19.         {
  20.             MoveObject();
  21.         }
  22.  
  23.         if (Input.GetButtonDown("Fire2"))    // Change to your desired input for releasing the object
  24.         {
  25.             ReleaseObject();
  26.         }
  27.     }
  28.  
  29.     void SelectObject()
  30.     {
  31.         RaycastHit hit;
  32.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // Use ray from camera to detect object under mouse
  33.  
  34.         if (Physics.Raycast(ray, out hit, range, interactableLayer))
  35.         {
  36.             if (hit.collider != null)
  37.             {
  38.                 selectedObject = hit.transform;
  39.             }
  40.         }
  41.     }
  42.  
  43.     void MoveObject()
  44.     {
  45.         // Get mouse position in world space
  46.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  47.         Plane plane = new Plane(Vector3.up, Vector3.zero); // Assuming Y is the up axis
  48.  
  49.         float distance;
  50.         if (plane.Raycast(ray, out distance))
  51.         {
  52.             Vector3 targetPosition = ray.GetPoint(distance);
  53.             selectedObject.position = Vector3.Lerp(selectedObject.position, targetPosition, Time.deltaTime * moveSpeed);
  54.         }
  55.     }
  56.  
  57.     void ReleaseObject()
  58.     {
  59.         selectedObject = null;
  60.     }
  61. }
  62.  
Tags: Script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement