afenkurtz

TableControl

Jan 11th, 2024
1,505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.38 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class TableControl : MonoBehaviour
  7. {
  8.     public List<bool> AtMin { get; private set; } = new List<bool>(new bool[3]);    // list of 3 bools to keep track of axis bounds when iterating through Vector3 input components
  9.     public List<bool> AtMax { get; private set; } = new List<bool>(new bool[3]);    // ...
  10.  
  11.     [SerializeField] Vector3 _axisInput = Vector3.zero;
  12.     [SerializeField] Vector3 _axisCalculatedMovement = Vector3.zero;
  13.  
  14.     [Header("CNC GameObjects")]
  15.     [SerializeField] GameObject x_axis;
  16.     [SerializeField] GameObject y_axis;
  17.     [SerializeField] GameObject z_axis;
  18.     [SerializeField] GameObject pins;
  19.  
  20.     [Header("Control Variables")]
  21.     [SerializeField] float moveSpeed = 2.5f;
  22.     [SerializeField] float plungeSpeed = 0.5f;  // for z axis only
  23.  
  24.     [Header("Pins")]
  25.     public bool pinsUp = true;
  26.     public float pins_min;  // distance to translate pins
  27.  
  28.  
  29.     /*
  30.      *
  31.      * UPDATE
  32.      *
  33.      */
  34.     void Update()
  35.     {
  36.         _axisInput = GetPlayerInput();
  37.  
  38.         _axisCalculatedMovement = BoundsCheck();
  39.  
  40.         _axisCalculatedMovement = MovementMultipliers();
  41.  
  42.         _axisCalculatedMovement *= Time.deltaTime;
  43.  
  44.         x_axis.transform.Translate(0, 0, _axisCalculatedMovement.x);
  45.         y_axis.transform.Translate(_axisCalculatedMovement.y, 0, 0);
  46.         z_axis.transform.Translate(0, _axisCalculatedMovement.z, 0);
  47.     }
  48.  
  49.  
  50.  
  51.     /*
  52.      *
  53.      * PRIVATE METHODS
  54.      *
  55.      */
  56.     /// <summary>
  57.     /// X = "Horizontal", Y = "Vertical", and Z = "Depth")
  58.     /// </summary>
  59.     /// <returns>New Vector3 from axis inputs.</returns>
  60.     private Vector3 GetPlayerInput()
  61.     {
  62.         if (Input.GetKeyDown(KeyCode.P))
  63.         {
  64.             TogglePins();
  65.         }
  66.        
  67.         return new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), Input.GetAxis("Depth"));
  68.     }
  69.  
  70.  
  71.     /// <summary>
  72.     /// Translates pins GameObject pos or neg by pins_min and sets bool to keep track.
  73.     /// </summary>
  74.     private void TogglePins()
  75.     {
  76.         if (pinsUp)
  77.         {
  78.             pins.transform.Translate(0, -pins_min, 0);
  79.             pinsUp = false;
  80.         }
  81.         else
  82.         {
  83.             pins.transform.Translate(0, pins_min, 0);
  84.             pinsUp = true;
  85.         }
  86.     }
  87.  
  88.  
  89.     /// <summary>
  90.     /// Contains the gantry, carriage, and device within predefined bounds.
  91.     /// <para>Checks if the gantry, carriage, and device can move in the direction being inputed.</para>
  92.     /// <para>Axis float gets carried over to calculation unless an axis is at its positive or negative bounds.</para>
  93.     /// <para>Collision detection on the x_axis, y_axis, and z_axis GameObjects calls SetMin() and or SetMax() to contstrain movement within x/y/z_min/max bounds.</para>
  94.     /// </summary>
  95.     /// <returns>Vector3 movement direcitons for each axis.</returns>
  96.     private Vector3 BoundsCheck()
  97.     {
  98.         _axisCalculatedMovement = Vector3.zero;
  99.  
  100.         // loop through x, y, and z components of the input and new calculation vectors
  101.         for (int i = 0; i <= 2; i++)
  102.         {
  103.             // if inputing to move negatively (left/down),
  104.             if (_axisInput[i] < -0.01f)
  105.             {
  106.                 if (AtMin[i])
  107.                     // essentially ignores input this frame if at bounds
  108.                     _axisCalculatedMovement[i] = 0.0f;
  109.                 else
  110.                     // copies input component to maintain variable speed control
  111.                     _axisCalculatedMovement[i] = _axisInput[i];
  112.             }
  113.             // if inputing to move positively (right/up)
  114.             else if (_axisInput[i] > 0.01f)
  115.             {
  116.                 if (AtMax[i])
  117.                     _axisCalculatedMovement[i] = 0.0f;
  118.                 else
  119.                     _axisCalculatedMovement[i] = _axisInput[i];
  120.             }
  121.             else
  122.                 _axisCalculatedMovement[i] = 0.0f;
  123.         }
  124.  
  125.         return _axisCalculatedMovement;
  126.     }
  127.  
  128.     /// <summary>
  129.     /// Applies multipliers depending on axis.
  130.     /// </summary>
  131.     /// <returns>New Vector3 calculation with movement multipliers applied.</returns>
  132.     private Vector3 MovementMultipliers()
  133.     {
  134.         return new Vector3(_axisCalculatedMovement.x * moveSpeed,
  135.                             _axisCalculatedMovement.y * -moveSpeed, // gets inverted due to local rotational difference to x
  136.                             _axisCalculatedMovement.z * plungeSpeed);
  137.     }
  138.  
  139.  
  140.     /*
  141.      *
  142.      * PUBLIC METHODS
  143.      *
  144.      */
  145.     /// <summary>
  146.     /// Called after an axis collides with its min trigger.
  147.     /// <para>Takes in a component of a Vector3 and toggles a bool component whose position refers to the component of a Vector3.</para>
  148.     /// </summary>
  149.     /// <param name="position">The 0-indexed component of a Vector3.</param>
  150.     public void SetMin(int position)
  151.     {
  152.         AtMin[position] ^= true;
  153.     }
  154.  
  155.     /// <summary>
  156.     /// Called after an axis collides with its max trigger.
  157.     /// <para>Takes in a component of a Vector3 and toggles a bool component whose position refers to the component of a Vector3.</para>
  158.     /// </summary>
  159.     /// <param name="position">The 0-indexed component of a Vector3.</param>
  160.     public void SetMax(int position)
  161.     {
  162.         AtMax[position] ^= true;
  163.     }
  164. }
Tags: Unity cnc
Advertisement
Add Comment
Please, Sign In to add comment