-
-
Save BobsiUnity/9e7379e7f6018ed10b32fa9460d0fe0e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using UnityEngine; | |
| using Random = UnityEngine.Random; | |
| public class CubeTransformer : MonoBehaviour | |
| { | |
| [SerializeField] private Vector3 minInitialPosition; | |
| [SerializeField] private Vector3 maxInitialPosition; | |
| [SerializeField] private Vector2 heightRange = new Vector2(-5, 5); | |
| [SerializeField] private Vector2 rotationSpeedRange = new Vector2(-60, 60); | |
| [SerializeField] private Vector3 scaleRange = Vector3.one; | |
| private Vector3 _initialPosition; | |
| private Vector3 _initialRotation; | |
| private Vector3 _initialScale; | |
| private float _rotationSpeed; | |
| private float _currentMoveTime = 0; | |
| private float _scaleTime; | |
| private void Awake() | |
| { | |
| _initialPosition = new Vector3( | |
| Random.Range(minInitialPosition.x, maxInitialPosition.x), | |
| Random.Range(minInitialPosition.y, maxInitialPosition.y), | |
| Random.Range(minInitialPosition.z, maxInitialPosition.z) | |
| ); | |
| _initialRotation = new Vector3( | |
| Random.Range(rotationSpeedRange.x, rotationSpeedRange.y), | |
| Random.Range(rotationSpeedRange.x, rotationSpeedRange.y), | |
| Random.Range(rotationSpeedRange.x, rotationSpeedRange.y) | |
| ); | |
| _initialScale = new Vector3( | |
| 1 + Random.Range(scaleRange.x, scaleRange.y), | |
| 1 + Random.Range(scaleRange.x, scaleRange.y), | |
| 1 + Random.Range(scaleRange.x, scaleRange.y) | |
| ); | |
| transform.position = _initialPosition; | |
| transform.rotation = Quaternion.Euler(_initialRotation); | |
| transform.localScale = _initialScale; | |
| _rotationSpeed = Random.Range(rotationSpeedRange.x, rotationSpeedRange.y); | |
| _currentMoveTime = Random.Range(0f, 10f); | |
| _scaleTime = Random.Range(0.5f, 2f); | |
| } | |
| private void Update() | |
| { | |
| _currentMoveTime += Time.deltaTime; | |
| var height = Mathf.Lerp(heightRange.x, heightRange.y, Mathf.PingPong(_currentMoveTime, 1)); | |
| transform.position = new Vector3( | |
| transform.position.x, | |
| height, | |
| transform.position.z | |
| ); | |
| transform.Rotate(_initialRotation * (_rotationSpeed * Time.deltaTime)); | |
| transform.localScale = _initialScale * Mathf.PingPong(_currentMoveTime, _scaleTime); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment