Advertisement
GUPPYYYY

Pen drawing on paper

Mar 26th, 2025
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Valve.VR.InteractionSystem;
  5.  
  6. public class Pen : MonoBehaviour
  7. {
  8.     [Header("Pen Properties")]
  9.     public Transform tip;
  10.     public Material drawingMaterial;
  11.     public Material tipMaterial;
  12.     [Range(0.01f, 0.1f)]
  13.     public float penWidth = 0.01f;
  14.     public Color[] penColors;
  15.  
  16.     [Header("Grabbable")]
  17.     public Interactable interactable;
  18.  
  19.     private LineRenderer currentDrawing;
  20.     private int index;
  21.     private int currentColorIndex;
  22.     private bool isBeingHeld = false;
  23.     private bool isDrawingOnPaper = false;
  24.  
  25.     private void Start()
  26.     {
  27.         currentColorIndex = 0;
  28.         tipMaterial.color = penColors[currentColorIndex];
  29.  
  30.         if (interactable == null)
  31.         {
  32.           interactable = GetComponent<Interactable>();
  33.         }
  34.         interactable.onAttachedToHand += OnGrabbed;
  35.         interactable.onDetachedFromHand += OnReleased;
  36.     }
  37.  
  38.     private void OnGrabbed(Hand hand)
  39.     {
  40.         isBeingHeld = true;
  41.     }
  42.  
  43.     private void OnReleased(Hand hand)
  44.     {
  45.         isBeingHeld = false;
  46.         if (currentDrawing != null)
  47.         {
  48.             currentDrawing = null;
  49.         }
  50.     }
  51.  
  52.     private void Draw()
  53.     {
  54.         if (currentDrawing == null)
  55.         {
  56.    
  57.             index = 0;
  58.             currentDrawing = new GameObject("Drawing").AddComponent<LineRenderer>();
  59.             currentDrawing.material = drawingMaterial;
  60.             currentDrawing.startColor = currentDrawing.endColor = penColors[currentColorIndex];
  61.             currentDrawing.startWidth = currentDrawing.endWidth = penWidth;
  62.             currentDrawing.positionCount = 1;
  63.             currentDrawing.SetPosition(0, tip.transform.position);
  64.         }
  65.         else
  66.         {
  67.            
  68.             var currentPosition = currentDrawing.GetPosition(index);
  69.             if (Vector3.Distance(currentPosition, tip.position) > 0.01f)
  70.             {
  71.                 index++;
  72.                 currentDrawing.positionCount = index + 1;
  73.                 currentDrawing.SetPosition(index, tip.position);
  74.             }
  75.         }
  76.     }
  77.  
  78.     private void Update()
  79.     {  
  80.         if (isBeingHeld && isDrawingOnPaper)
  81.         {
  82.             Draw();
  83.         }
  84.         else if (currentDrawing != null)
  85.         {
  86.            
  87.             currentDrawing = null;
  88.         }  
  89.     }
  90.  
  91.     private void OnTriggerEnter(Collider other)
  92.     {
  93.        
  94.         if (other.CompareTag("Paper"))
  95.         {
  96.             isDrawingOnPaper = true;
  97.         }
  98.     }
  99.  
  100.     private void OnTriggerExit(Collider other)
  101.     {
  102.        
  103.         if (other.CompareTag("Paper"))
  104.         {
  105.             isDrawingOnPaper = false;
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement