【发布时间】:2021-07-25 20:41:46
【问题描述】:
我目前正在制作 2D 游戏,并添加了一个围绕中心旋转的平台。现在我想要,您可以选择要围绕中心旋转多少个平台。就像在带有电路点的屏幕截图上一样。我对编码不是很感兴趣,请问您可以将其添加到我当前的脚本中吗?
谢谢!
当前脚本:
using UnityEngine;
public class Rotation_Object_System : MonoBehaviour
{
[SerializeField]
private Transform rotationCenter;
public GameObject rotationObject;
[SerializeField]
private float rotationRadius = 2f, angularSpeed = 2f;
public bool ClockwiseRotation;
public bool HideRotationCenter;
private float posX, posY, angle = 0f;
private void Start()
{
Loading_Initial_Parameters();
}
private void Update()
{
// Rotation
posX = rotationCenter.position.x + Mathf.Cos(angle) * rotationRadius;
posY = rotationCenter.position.y + Mathf.Sin(angle) * rotationRadius;
rotationObject.transform.position = new Vector2(posX, posY);
float angularMovement = Time.deltaTime * angularSpeed;
if (ClockwiseRotation)
angle -= angularMovement;
else
angle += angularMovement;
if (angle >= 360f)
angle = 0f;
}
private void Loading_Initial_Parameters()
{
if (rotationCenter.TryGetComponent<SpriteRenderer>(out var rotationCenterSpriteRenderer))
{
rotationCenterSpriteRenderer.enabled = !HideRotationCenter;
}
else
{
Debug.LogWarning(
$"Rotation Center ({rotationCenter.name}) does NOT HAVE " +
$"a SpriteRenderer Component to hide/show");
}
}
}
【问题讨论】: