Advertisement
JGroxz

Digital Manufacturing (2025)

Feb 19th, 2025 (edited)
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | None | 0 0
  1. // Digital Manufacturing (2025)
  2.  
  3.  
  4.  
  5. // Script 1: RobotController
  6.  
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using UnityEngine;
  11.  
  12. public class RobotController : MonoBehaviour
  13. {
  14.     [Serializable]
  15.     public class JointData
  16.     {
  17.         public Transform transform;
  18.        
  19.         public float velocity = 30f;
  20.         public float currentAngle = 0f;
  21.         public float minAngle = -90f;
  22.         public float maxAngle = 90f;
  23.     }
  24.    
  25.     public List<JointData> joints = new List<JointData>();
  26.     public int selectedJointIndex = 0;
  27.  
  28.     private bool shouldMoveLeft = false;
  29.     private bool shouldMoveRight = false;
  30.  
  31.     public JointData SelectedJoint => joints[selectedJointIndex];
  32.    
  33.     // Update is called once per frame
  34.     void Update()
  35.     {
  36.         float angleDelta = 0f;
  37.  
  38.         angleDelta = ApplyKeyboardInputs(angleDelta);
  39.         angleDelta = ApplyUiInputs(angleDelta);
  40.         angleDelta = ApplyJointLimits(angleDelta);
  41.        
  42.         SelectedJoint.transform.Rotate(0f, 0f, angleDelta);
  43.         SelectedJoint.currentAngle += angleDelta;
  44.     }
  45.  
  46.     private float ApplyKeyboardInputs(float angleDelta)
  47.     {
  48.         bool isLeftPressed = Input.GetKey(KeyCode.LeftArrow);
  49.         if (isLeftPressed)
  50.         {
  51.             angleDelta = SelectedJoint.velocity * Time.deltaTime;
  52.         }
  53.  
  54.         bool isRightPressed = Input.GetKey(KeyCode.RightArrow);
  55.         if (isRightPressed)
  56.         {
  57.             angleDelta = -SelectedJoint.velocity * Time.deltaTime;
  58.         }
  59.  
  60.         bool isSpacePressed = Input.GetKeyDown(KeyCode.Space);
  61.         if (isSpacePressed)
  62.         {
  63.             SelectNextJoint();
  64.         }
  65.  
  66.         return angleDelta;
  67.     }
  68.  
  69.     private float ApplyUiInputs(float angleDelta)
  70.     {
  71.         if (shouldMoveLeft)
  72.         {
  73.             angleDelta = SelectedJoint.velocity * Time.deltaTime;
  74.         }
  75.  
  76.         if (shouldMoveRight)
  77.         {
  78.             angleDelta = -SelectedJoint.velocity * Time.deltaTime;
  79.         }
  80.  
  81.         return angleDelta;
  82.     }
  83.    
  84.     private float ApplyJointLimits(float angleDelta)
  85.     {
  86.         if (SelectedJoint.currentAngle + angleDelta < SelectedJoint.minAngle)
  87.         {
  88.             angleDelta = SelectedJoint.minAngle - SelectedJoint.currentAngle;
  89.         }
  90.  
  91.         if (SelectedJoint.currentAngle + angleDelta > SelectedJoint.maxAngle)
  92.         {
  93.             angleDelta = SelectedJoint.maxAngle - SelectedJoint.currentAngle;
  94.         }
  95.  
  96.         return angleDelta;
  97.     }
  98.  
  99.     public void SelectNextJoint()
  100.     {
  101.         selectedJointIndex += 1;
  102.  
  103.         if (selectedJointIndex >= joints.Count)
  104.         {
  105.             selectedJointIndex = 0;
  106.         }
  107.     }
  108.    
  109.     public void StartMovingLeft()
  110.     {
  111.         shouldMoveLeft = true;
  112.         shouldMoveRight = false;
  113.     }
  114.  
  115.     public void StartMovingRight()
  116.     {
  117.         shouldMoveLeft = false;
  118.         shouldMoveRight = true;
  119.     }
  120.  
  121.     public void Stop()
  122.     {
  123.         shouldMoveLeft = false;
  124.         shouldMoveRight = false;
  125.     }
  126. }
  127.  
  128.  
  129.  
  130. // XR Packages
  131. // VR:
  132. //   Tilia Spatial Simulator: https://github.com/ExtendRealityLtd/Tilia.CameraRigs.SpatialSimulator.Unity
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement