_
This commit is contained in:
64
Assets/Scripts/Rotation.cs
Normal file
64
Assets/Scripts/Rotation.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Rotation : MonoBehaviour
|
||||
{
|
||||
|
||||
public GameObject rotation_center;
|
||||
public bool static_center = true;
|
||||
public bool right_rotation = true;
|
||||
public bool flipping = false;
|
||||
[Range(0.1f, 8)] [SerializeField] private float speed = 1;
|
||||
[Range(0, (float)Math.PI * 2)] [SerializeField] private float flip_angle = 0;
|
||||
|
||||
private float angle;
|
||||
private float magnitude;
|
||||
private Vector3 rotation_center_position;
|
||||
private Vector3 object_position;
|
||||
private float[] centralized_position;
|
||||
private float flipped_angle = 0;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
object_position = transform.position;
|
||||
rotation_center_position = rotation_center.transform.position;
|
||||
|
||||
centralized_position = new float[] {object_position[0] - rotation_center_position[0], object_position[1] - rotation_center_position[1]};
|
||||
|
||||
angle = (float)Math.Atan2(centralized_position[0], centralized_position[1]);
|
||||
magnitude = (float)Math.Sqrt(Math.Pow(centralized_position[0], 2) + Math.Pow(centralized_position[1], 2));
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (!static_center) {
|
||||
rotation_center_position = rotation_center.transform.position;
|
||||
}
|
||||
|
||||
if (right_rotation) {
|
||||
angle -= speed * Time.deltaTime;
|
||||
} else {
|
||||
angle += speed * Time.deltaTime;
|
||||
}
|
||||
|
||||
if (flipping) {
|
||||
flipped_angle += speed * Time.deltaTime;
|
||||
if (flipped_angle > flip_angle) {
|
||||
right_rotation = !right_rotation;
|
||||
flipped_angle -= flip_angle;
|
||||
}
|
||||
}
|
||||
|
||||
centralized_position = new float[] {(float)Math.Cos(angle) * magnitude, (float)Math.Sin(angle) * magnitude};
|
||||
|
||||
object_position[0] = centralized_position[0] + rotation_center_position[0];
|
||||
object_position[1] = centralized_position[1] + rotation_center_position[1];
|
||||
|
||||
transform.position = object_position;
|
||||
}
|
||||
}
|
11
Assets/Scripts/Rotation.cs.meta
Normal file
11
Assets/Scripts/Rotation.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f4366df60f8ac7f428d235d55b5dfc13
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user