【发布时间】:2021-08-28 16:17:33
【问题描述】:
我目前正在以初学者的身份制作 2D 游戏。我想添加一个围绕中心旋转的平台。我已经在脚本中拥有了所有东西,但是隐藏平台的中心和顺时针旋转根本不起作用,所以我想问是否有人可以改进我的脚本。我还想让它像在统一资产商店的高级平台 2D 资产中一样,就像在屏幕截图上一样。所以你可以选择你的平台和中心。
谢谢!
截图:Advanced Platforms 2D Asset
当前脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotating_Platform : MonoBehaviour
{
[SerializeField]
Transform rotationCenter;
public GameObject rotationObject;
[SerializeField]
float rotationRadius = 2f, angularSpeed = 2f;
float posX, posY, angle = 0f;
private float rotZ;
public bool ClockwiseRotation;
// Update is called once per frame
void Update()
{
//Rotation
posX = rotationCenter.position.x + Mathf.Cos(angle) * rotationRadius;
posY = rotationCenter.position.y + Mathf.Sin(angle) * rotationRadius;
transform.position = new Vector2(posX, posY);
angle = angle + Time.deltaTime * angularSpeed;
if (angle >= 360f)
angle = 0f;
//Clockwise Rotation
if (ClockwiseRotation == false)
{
rotZ += Time.deltaTime * angularSpeed;
}
else
{
rotZ += -Time.deltaTime * angularSpeed;
}
}
public bool HideRotationCenter;
private void Loading_Initial_Parameters()
{
//Hiding Rotation Center
if (HideRotationCenter)
{
if (rotationCenter.GetComponent<SpriteRenderer>() != null)
{
rotationCenter.GetComponent<SpriteRenderer>().enabled = false;
}
else
{
Debug.LogWarning("Rotation Center (" + rotationCenter.name + ") does NOT HAVE a SpriteRenderer Component to hide");
}
}
else
{
//---Sprite renderers or Mesh renderers keep visible
}
}
}
【问题讨论】:
-
3 个问题。 1.- 如果您使用
transform.position = new Vector2(posX, posY);在旋转中设置平台位置,那么rotZ是什么? 2.-“顺时针旋转根本不起作用”是什么意思?是逆时针旋转工作还是平台不动?结果如何?控制台中的任何错误? 3.- 您是否将Rotating_Platform脚本附加到平台游戏对象? -
@rustyBucketBay 1. rotZ 可能根本不重要,因为我粘贴了顺时针旋转并隐藏了中心。 2. 是的,顺时针旋转有效,逆时针旋转无效。并且控制台没有错误 3. 不,它附加到一个空的 GameObject 以使其排列更清晰。