Guest User

Untitled

a guest
Dec 26th, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. [RequireComponent(typeof(PolygonCollider2D))]
  5. public class AxisObject : MonoBehaviour {
  6.     private Plane dragPlane;
  7.     private Vector3 offset;
  8.     private Camera myMainCamera;
  9.     Vector3 initial;
  10.  
  11.     [Header("Axis")]
  12.     public Axis axis;
  13.  
  14.     [Header("Response Colors")]
  15.     [SerializeField] SpriteRenderer MyRenderer;
  16.     [SerializeField] Color NormalColor;
  17. [SerializeField] Color HighlightColor;
  18. [SerializeField] Color ClickOrDragColor;
  19.  
  20.     void Start()
  21.     {
  22.         myMainCamera = Camera.main;
  23.        
  24.     }
  25.     [SerializeField] GameObject Thing;
  26.     public void ThingToDrag(GameObject newLink) {
  27.         Thing = newLink;
  28.     }
  29.  
  30.     void OnMouseEnter() {
  31.         MyRenderer.color = HighlightColor;
  32.     }
  33.  
  34.     void OnMouseDown()
  35.     {
  36.         //just getting all the shit ready
  37.         MyRenderer.color = ClickOrDragColor;
  38.         dragPlane = new Plane(myMainCamera.transform.forward, transform.position);
  39.         Ray camRay = myMainCamera.ScreenPointToRay(Input.mousePosition);
  40.         float planeDist;
  41.         dragPlane.Raycast(camRay, out planeDist);
  42.         offset = transform.position - camRay.GetPoint(planeDist);
  43.     }
  44.  
  45.     void OnMouseDrag() //this happens when I drag something related to the object (obviously)
  46. {
  47.     MyRenderer.color = ClickOrDragColor;
  48.     Ray camRay = myMainCamera.ScreenPointToRay(Input.mousePosition);
  49.     float planeDist;
  50.     dragPlane.Raycast(camRay, out planeDist);
  51.  
  52.     // Get the new position with the offset
  53.     Vector3 newPosition = camRay.GetPoint(planeDist) + offset;
  54.  
  55.     // Calculate the difference in position
  56.     Vector3 deltaPosition = newPosition - initial;
  57.  
  58.     // Set the object's position based on the axis
  59.     if (axis == Axis.X)
  60.         Thing.transform.position = new Vector3(newPosition.x, Thing.transform.position.y, Thing.transform.position.z);
  61.     else if (axis == Axis.Y)
  62.         Thing.transform.position = new Vector3(Thing.transform.position.x, newPosition.y, Thing.transform.position.z);
  63.     else if (axis == Axis.Z)
  64.         Thing.transform.position = new Vector3(Thing.transform.position.x, Thing.transform.position.y, newPosition.z);
  65.  
  66.     // Update the initial position for the next frame
  67.     initial = newPosition;
  68. }
  69.  
  70.  
  71.     void OnMouseExit() {
  72.                 MyRenderer.color = NormalColor;
  73.     }
  74.  
  75.     void OnMouseUp() {
  76.                 MyRenderer.color = NormalColor;
  77.     }
  78.  
  79.  
  80.  
  81. }
  82.  
  83. public enum Axis {
  84.     X, Y, Z
  85. }
Tags: cs
Advertisement
Add Comment
Please, Sign In to add comment