Advertisement
Dieton

TransformThis

May 18th, 2023
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | Gaming | 0 0
  1. /*
  2. By: Nathan Rumsey
  3. Engineer 3d llc
  4. 2022
  5. */
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9.  
  10. public class TransformThis : MonoBehaviour
  11. {
  12.     public Transform transformThis;
  13.     [Header("Transform")]
  14.     public bool transformIO = false;
  15.     public Vector3 transSpeed;
  16.  
  17.     [Header("Rotate")]
  18.     public bool rotateIO = false;
  19.     public Vector3 rotateSpeed;
  20.  
  21.     [Header("lookAt")]
  22.     public bool lookAtIO = false;
  23.     public Transform lookAtPoint;
  24.     public bool lookAtInvertIO = false;
  25.  
  26.     [Header("Randome")]
  27.     public bool randomeIO = false;
  28.     public Transform randomlyMoveThis;
  29.     public Vector2 MinMaxPositionX = new Vector2(-1,1);
  30.     public Vector2 MinMaxPositionY = new Vector2(-1, 1);
  31.     public Vector2 MinMaxPositionZ = new Vector2(-1, 1);
  32.     public float movementIntervals = 1;
  33.  
  34.     [Header("PingPong"), Tooltip("requires parent maby")]
  35.     public bool pingPongIO = false;
  36.     Vector3 startPosition;
  37.     Transform parentTransform;
  38.  
  39.     float realtime;
  40.  
  41.     // Start is called before the first frame update
  42.     void Start()
  43.     {
  44.         if (pingPongIO) {
  45.             if (transform.parent) {
  46.                 parentTransform = transform.parent;
  47.             }
  48.             startPosition = transform.position;
  49.         }
  50.        
  51.  
  52.         if (!transformThis)
  53.         {
  54.             transformThis = this.transform;
  55.         }
  56.  
  57.         if (randomeIO) {
  58.         StartCoroutine(randomeMove());
  59.         }
  60.     }
  61.  
  62.     // Update is called once per frame
  63.     void Update()
  64.     {
  65.         realtime = Time.deltaTime;
  66.         if (transformIO)
  67.             transformThis.Translate(transSpeed.x * realtime, transSpeed.y * realtime, transSpeed.z * realtime);
  68.         if (rotateIO)
  69.             transformThis.Rotate(rotateSpeed.x * realtime, rotateSpeed.y * realtime, rotateSpeed.z * realtime);
  70.         if (lookAtIO)
  71.             if (lookAtInvertIO)
  72.             {
  73.                 transformThis.rotation = Quaternion.LookRotation(lookAtPoint.position);
  74.             }
  75.             else
  76.             {
  77.                 transformThis.LookAt(lookAtPoint);
  78.             }
  79.  
  80.         if (pingPongIO ) {
  81.             PingPongAction();
  82.         }
  83.     }
  84.  
  85.     IEnumerator randomeMove() {
  86.         for (; ; ) {
  87.             RandMove();
  88.             yield return new WaitForSeconds(movementIntervals);
  89.         }
  90.        
  91.     }
  92.  
  93.     public void RandMove() {
  94.         randomlyMoveThis.position = new Vector3(
  95.                     this.transform.position.x + Random.Range(MinMaxPositionX.x, MinMaxPositionX.y),
  96.                     this.transform.position.y + Random.Range(MinMaxPositionY.x, MinMaxPositionY.y),
  97.                     this.transform.position.z + Random.Range(MinMaxPositionZ.x, MinMaxPositionZ.y));
  98.     }
  99.  
  100.     void PingPongAction() {
  101.         if (parentTransform)
  102.         {
  103.             transform.position = new Vector3(
  104.             parentTransform.position.x,
  105.             parentTransform.position.y + (Mathf.Sin(Time.time * 5) / 3),
  106.             parentTransform.position.z);
  107.         }
  108.         else {
  109.             transform.position = new Vector3(
  110.                 startPosition.x,
  111.                 startPosition.y + (Mathf.Sin(Time.time * 5) / 3),
  112.                 startPosition.z);
  113.         }
  114.        
  115.     }
  116. }
  117.  
Tags: Unity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement