Created
July 27, 2021 07:21
-
-
Save Aphellirus/176f9776dc7ef6119a5683f3910d03e5 to your computer and use it in GitHub Desktop.
Every object in a Scene has a Transform. We can use it to store and manipulate the position, rotation and scale of the object.
This file contains 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.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class ObjectTransform : MonoBehaviour | |
{ | |
// Start is called before the first frame update | |
private Vector3 startPosition; | |
public bool changePosition, moveInCircles; | |
public float positionSpeed; | |
public int positionX, positionY, positionZ; | |
public bool changeRotation; | |
public int rotationSpeed; | |
public int rotationX, rotationY, rotationZ; | |
public bool changeScale; | |
public float scaleSpeed; | |
public int maxSize; | |
void Start() | |
{ | |
startPosition = transform.position; | |
positionSpeed = 1; | |
rotationSpeed = 2; | |
scaleSpeed = .5f; | |
maxSize = 5; | |
} | |
void TransformPosition() | |
{ | |
if (moveInCircles) | |
{ | |
// transform.position = startPosition + new Vector3(Mathf.Sin(Time.deltaTime * positionSpeed), 0.0f, 0.0f); | |
float sinAnim = Mathf.Sin(Time.time * positionSpeed); | |
transform.position = startPosition + new Vector3(sinAnim * positionX, sinAnim * positionY, sinAnim * positionZ); | |
} | |
if (!moveInCircles) | |
{ | |
transform.Translate(new Vector3(positionX, positionY, positionZ) * positionSpeed * Time.deltaTime); | |
} | |
} | |
void TransformRotation() | |
{ | |
transform.Rotate(new Vector3(rotationX, rotationY, rotationZ) * rotationSpeed * Time.deltaTime); | |
} | |
void TransformScale() | |
{ | |
transform.localScale += new Vector3(scaleSpeed, scaleSpeed, scaleSpeed) * Time.smoothDeltaTime; | |
if (transform.localScale.x >= maxSize) | |
{ | |
transform.localScale = new Vector3(maxSize, maxSize, maxSize); | |
} | |
if (transform.localScale.x < -maxSize) | |
{ | |
transform.localScale = -1 * new Vector3(maxSize, maxSize, maxSize); | |
} | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
if (changePosition) | |
{ | |
TransformPosition(); | |
} | |
if (changeRotation) | |
{ | |
TransformRotation(); | |
} | |
if (changeScale) | |
{ | |
TransformScale(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment