Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class TableControl : MonoBehaviour
- {
- 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
- public List<bool> AtMax { get; private set; } = new List<bool>(new bool[3]); // ...
- [SerializeField] Vector3 _axisInput = Vector3.zero;
- [SerializeField] Vector3 _axisCalculatedMovement = Vector3.zero;
- [Header("CNC GameObjects")]
- [SerializeField] GameObject x_axis;
- [SerializeField] GameObject y_axis;
- [SerializeField] GameObject z_axis;
- [SerializeField] GameObject pins;
- [Header("Control Variables")]
- [SerializeField] float moveSpeed = 2.5f;
- [SerializeField] float plungeSpeed = 0.5f; // for z axis only
- [Header("Pins")]
- public bool pinsUp = true;
- public float pins_min; // distance to translate pins
- /*
- *
- * UPDATE
- *
- */
- void Update()
- {
- _axisInput = GetPlayerInput();
- _axisCalculatedMovement = BoundsCheck();
- _axisCalculatedMovement = MovementMultipliers();
- _axisCalculatedMovement *= Time.deltaTime;
- x_axis.transform.Translate(0, 0, _axisCalculatedMovement.x);
- y_axis.transform.Translate(_axisCalculatedMovement.y, 0, 0);
- z_axis.transform.Translate(0, _axisCalculatedMovement.z, 0);
- }
- /*
- *
- * PRIVATE METHODS
- *
- */
- /// <summary>
- /// X = "Horizontal", Y = "Vertical", and Z = "Depth")
- /// </summary>
- /// <returns>New Vector3 from axis inputs.</returns>
- private Vector3 GetPlayerInput()
- {
- if (Input.GetKeyDown(KeyCode.P))
- {
- TogglePins();
- }
- return new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), Input.GetAxis("Depth"));
- }
- /// <summary>
- /// Translates pins GameObject pos or neg by pins_min and sets bool to keep track.
- /// </summary>
- private void TogglePins()
- {
- if (pinsUp)
- {
- pins.transform.Translate(0, -pins_min, 0);
- pinsUp = false;
- }
- else
- {
- pins.transform.Translate(0, pins_min, 0);
- pinsUp = true;
- }
- }
- /// <summary>
- /// Contains the gantry, carriage, and device within predefined bounds.
- /// <para>Checks if the gantry, carriage, and device can move in the direction being inputed.</para>
- /// <para>Axis float gets carried over to calculation unless an axis is at its positive or negative bounds.</para>
- /// <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>
- /// </summary>
- /// <returns>Vector3 movement direcitons for each axis.</returns>
- private Vector3 BoundsCheck()
- {
- _axisCalculatedMovement = Vector3.zero;
- // loop through x, y, and z components of the input and new calculation vectors
- for (int i = 0; i <= 2; i++)
- {
- // if inputing to move negatively (left/down),
- if (_axisInput[i] < -0.01f)
- {
- if (AtMin[i])
- // essentially ignores input this frame if at bounds
- _axisCalculatedMovement[i] = 0.0f;
- else
- // copies input component to maintain variable speed control
- _axisCalculatedMovement[i] = _axisInput[i];
- }
- // if inputing to move positively (right/up)
- else if (_axisInput[i] > 0.01f)
- {
- if (AtMax[i])
- _axisCalculatedMovement[i] = 0.0f;
- else
- _axisCalculatedMovement[i] = _axisInput[i];
- }
- else
- _axisCalculatedMovement[i] = 0.0f;
- }
- return _axisCalculatedMovement;
- }
- /// <summary>
- /// Applies multipliers depending on axis.
- /// </summary>
- /// <returns>New Vector3 calculation with movement multipliers applied.</returns>
- private Vector3 MovementMultipliers()
- {
- return new Vector3(_axisCalculatedMovement.x * moveSpeed,
- _axisCalculatedMovement.y * -moveSpeed, // gets inverted due to local rotational difference to x
- _axisCalculatedMovement.z * plungeSpeed);
- }
- /*
- *
- * PUBLIC METHODS
- *
- */
- /// <summary>
- /// Called after an axis collides with its min trigger.
- /// <para>Takes in a component of a Vector3 and toggles a bool component whose position refers to the component of a Vector3.</para>
- /// </summary>
- /// <param name="position">The 0-indexed component of a Vector3.</param>
- public void SetMin(int position)
- {
- AtMin[position] ^= true;
- }
- /// <summary>
- /// Called after an axis collides with its max trigger.
- /// <para>Takes in a component of a Vector3 and toggles a bool component whose position refers to the component of a Vector3.</para>
- /// </summary>
- /// <param name="position">The 0-indexed component of a Vector3.</param>
- public void SetMax(int position)
- {
- AtMax[position] ^= true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment