C# 如何在编辑器中为Unity中的旋转平台脚本添加列表
我目前正在制作一个2D游戏,我添加了一个平台,它围绕一个中心旋转。现在我想,你可以选择围绕中心旋转多少个平台。就像屏幕截图上的线路点。我不是很热衷于编码,所以你能把它添加到我当前的脚本中吗 谢谢 截图: 当前脚本:C# 如何在编辑器中为Unity中的旋转平台脚本添加列表,c#,unity3d,rotation,C#,Unity3d,Rotation,我目前正在制作一个2D游戏,我添加了一个平台,它围绕一个中心旋转。现在我想,你可以选择围绕中心旋转多少个平台。就像屏幕截图上的线路点。我不是很热衷于编码,所以你能把它添加到我当前的脚本中吗 谢谢 截图: 当前脚本: using UnityEngine; public class Rotation_Object_System : MonoBehaviour { [SerializeField] private Transform rotationCenter; pub
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");
}
}
}
使用UnityEngine;
公共类旋转对象系统:单行为
{
[序列化字段]
私有变换旋转中心;
公共游戏对象旋转对象;
[序列化字段]
专用浮动旋转半径=2f,角速度=2f;
公共图书馆顺时针旋转;
公共布尔HideRotationCenter;
专用浮点数posX,posY,角度=0f;
私有void Start()
{
加载初始参数();
}
私有void更新()
{
//轮换
posX=旋转中心位置x+数学坐标(角度)*旋转半径;
posY=旋转中心位置y+数学正弦(角度)*旋转半径;
rotationObject.transform.position=新向量2(posX,posY);
浮动角度移动=Time.deltaTime*角度速度;
if(顺时针旋转)
角度-=角度运动;
其他的
角度+=角度运动;
如果(角度>=360f)
角度=0f;
}
私有无效加载\初始\参数()
{
if(旋转中心.TryGetComponent(out var ROTATIONCENTERSPRITERENDER))
{
rotationCenterSpriteRenderer.enabled=!HideRotationCenter;
}
其他的
{
Debug.LogWarning(
$“旋转中心({rotationCenter.name})没有”+
$“要隐藏/显示的Spirterender组件”);
}
}
}
我认为一些更改将使这项工作适合您
谢谢你的回答,你有一些示例代码吗?