C# 如何让我的旋转平台脚本在Unity中工作?
我目前正在做一个2D游戏作为一个初学者。我想添加一个平台,它围绕一个中心旋转。我已经有了脚本中的所有内容,但是隐藏平台中心和顺时针旋转根本不起作用,所以我想问是否有人可以改进我的脚本。我还想让它像在unity资产商店的Advanced Platform 2D资产中一样,就像屏幕截图上一样。因此,您可以选择您的平台和中心 谢谢 截图: 当前脚本:C# 如何让我的旋转平台脚本在Unity中工作?,c#,unity3d,script,C#,Unity3d,Script,我目前正在做一个2D游戏作为一个初学者。我想添加一个平台,它围绕一个中心旋转。我已经有了脚本中的所有内容,但是隐藏平台中心和顺时针旋转根本不起作用,所以我想问是否有人可以改进我的脚本。我还想让它像在unity资产商店的Advanced Platform 2D资产中一样,就像屏幕截图上一样。因此,您可以选择您的平台和中心 谢谢 截图: 当前脚本: using System.Collections; using System.Collections.Generic; using UnityEngin
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
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类旋转平台:单一行为
{
[序列化字段]
变换旋转中心;
公共游戏对象旋转对象;
[序列化字段]
浮动旋转半径=2f,角速度=2f;
浮动posX,posY,角度=0f;
私人浮动旋转木马;
公共图书馆顺时针旋转;
//每帧调用一次更新
无效更新()
{
//轮换
posX=旋转中心位置x+数学坐标(角度)*旋转半径;
posY=旋转中心位置y+数学正弦(角度)*旋转半径;
transform.position=新向量2(posX,posY);
角度=角度+时间。增量时间*角度速度;
如果(角度>=360f)
角度=0f;
//顺时针旋转
如果(顺时针旋转==假)
{
rotZ+=时间。增量时间*角速度;
}
其他的
{
rotZ+=-Time.deltaTime*角速度;
}
}
公共布尔HideRotationCenter;
私有无效加载\初始\参数()
{
//隐藏旋转中心
如果(HideRotationCenter)
{
if(rotationCenter.GetComponent()!=null)
{
rotationCenter.GetComponent().enabled=false;
}
其他的
{
Debug.LogWarning(“旋转中心(“+rotationCenter.name+”)没有要隐藏的Spirterender组件”);
}
}
其他的
{
//---精灵渲染器或网格渲染器保持可见
}
}
}
为什么中心对象没有被隐藏
中心对象没有隐藏的原因是加载\u Initial\u Parameters()
方法在任何地方都不会被调用。您可以通过在Start()
方法中调用它来解决这个问题
为什么顺时针旋转不起作用
顺时针旋转
设置无法正常工作的原因是无论角度如何,角度
值都会朝同一方向移动。您可以通过添加一个if-else
语句来解决这个问题,该语句在一种情况下将减去值,在另一种情况下将它们相加
代码的修改版本
使用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组件”);
}
}
}
3个问题。1.-如果使用transform.position=new Vector2(posX,posY)在旋转中设置平台位置代码>那么什么是rotZ
??2.“顺时针旋转根本不起作用”是什么意思?是逆时针旋转工作还是平台不移动?结果如何?控制台中有错误吗?3.-您是否已将旋转_平台
脚本连接到平台游戏对象?@rustyBucketBay 1。它可能是,它rotZ一点都不重要,因为我粘贴了顺时针旋转,还隐藏了中心。2.是的,顺时针旋转有效,逆时针旋转无效。控制台3中没有错误。不,它被附加到一个空的游戏对象上,以使其排列更加清晰。谢谢,但所有内容都被隐藏,即使我取消选中了框。@JonathanXD12如果是这样,那么可能是因为场景本身中已禁用了游戏对象
或渲染组件。@JonathanXD12以防万一,更新了答案。脚本现在应该根据您的布尔值启用/禁用spritenderer
。但要确保游戏对象本身已启用。否则,请随意查看gameObject.SetActive()
方法,作为component.enabled
的替代方法。不,这太奇怪了,spriteender在每个启用的对象上都有。但可能是脚本工作不正常,因为我将它附加到一个空的游戏对象上,如屏幕截图所示:嗯。脚本似乎没有在任何地方使用rotationObject
。相反,它使用它所附加到的游戏对象的变换。更新了答案-替换了以下行:transform.position=newvector2(posX,posY)有限公司
using UnityEngine;
public class Rotating_Platform : 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");
}
}
}