[英]A problem with the rotation of objects in c#
using System.Collections.Generic;
using UnityEngine;
public class RotationPlasmaCast : MonoBehaviour
{
float LockPos = 0;
public Transform Squid;
float SquidRot = Squid.z;
void FixedUpdate()
{
transform.rotation = Quaternion.Euler(LockPos, LockPos, SquidRot.rotation);
}
}
我使用此代码使我的弹丸 (PlasmaCast) 的旋转与我的播放器 (Squid) 相同。 Unity 告诉我:
字段初始值设定项不能引用非静态字段、方法或属性 'RotationPlasmaCast.Squid
有谁知道为什么 Unity 给我一个错误?
假设您想存储 Squid 的 Z 旋转以在代码中的其他地方使用,您可以将初始旋转设置为 0,并在 FixedUpdate() 函数期间更新该值以匹配其当前旋转并将其传递到您的变换中,如下所示:
using System.Collections.Generic;
using UnityEngine;
public class RotationPlasmaCast : MonoBehaviour
{
float LockPos = 0;
public Transform Squid;
float SquidRot = 0;
void FixedUpdate()
{
SquidRot = Squid.rotation.z;
transform.rotation = Quaternion.Euler(LockPos, LockPos, SquidRot);
}
}
值得注意的是,根据您的游戏对象的层次结构,您可能需要使用“localRotation”而不是“rotation”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.