从UnityScript到C#代码
我在UnityScript中有一个代码,我想用C#转换它,但事实就是这样从UnityScript到C#代码,c#,unity3d,unityscript,C#,Unity3d,Unityscript,我在UnityScript中有一个代码,我想用C#转换它,但事实就是这样 Assets/My Scripts/projectileShot2.cs(23,52): error CS0019: Operator * cannot be applied to operands of type 'UnityEngine.Quaternion' and 'float' Assets/My Scripts/projectileShot2.cs(22,36): error CS0029: Cannot i
Assets/My Scripts/projectileShot2.cs(23,52): error CS0019: Operator * cannot be applied to operands of type 'UnityEngine.Quaternion' and 'float'
Assets/My Scripts/projectileShot2.cs(22,36): error CS0029: Cannot implicitly convert type 'UnityEngine.Vector3' to 'UnityEngine.Quaternion'
Assets/My Scripts/projectileShot2.cs(21,35): error CS0266: Cannot implicitly convert type 'UnityEngine.Object' to 'UnityEngine.Transform'. An explicit conversion exists (are you missing a cast?)
这是UnityScript的代码
#pragma strict
function Start () {
}
var canonPrefab : Transform;
var speed = 0f ;
var angle = 0f;
var time = 5.0f;
function Update(){
if(Input.GetButtonDown("Fire1"))
{
var canon: Transform = Instantiate (canonPrefab, transform.position,Quaternion.identity);
var shootDir = Quaternion.Euler(0, 0, angle) * Vector3.right;
canon.rigidbody.velocity = shootDir * speed;
Destroy(canon, 5.0f);
}
if(Input.GetKey (KeyCode.Q)){
transform.Rotate (new Vector3(0, 0, 30.0f) * Time.deltaTime);
angle += 1;
}
if(Input.GetKey (KeyCode.E)){
transform.Rotate (new Vector3(0, 0, -30.0f) * Time.deltaTime);
angle -= 1;
}
}
这就是C代码
我仍然是这个脚本的新手,仍然不熟悉数据类型。我希望你能帮助我。谢谢 我在工作中多次遇到这种情况,并大量使用了它。您让它编译成程序集(库/ScriptAssemblies/assembly-*.dll文件),然后使用ILSpy将结果读取并反编译为您选择的语言(如C#) 它有一些bug和怪癖,您仍然需要克服,但由于它为您完成了所有繁重的工作,因此您只需手动调整以适应代码的其余部分 编辑1: 在第23行,您有:
canon.rigidbody.velocity = shootDir * speed;
shootDir
是一个四元数,虽然您可以乘以Vector3
(更多说明),但不能乘以速度。尝试speed*Vector3.前进或其他方向
编辑2:
在第21行,您需要将其从实例化(cannonPrefab,
更改为实例化(cannonPrefab.gameObject,
编辑3:
我不完全确定第22行,但我想知道如果你把所有内容都包装到括号中的=
符号上,会发生什么,以及这是否会消除错误/警告。如果你在正确的数据类型上遇到问题,你可以随时检查。我在下面的答案中链接了相应的站点。让我们来看看检查每个错误以及编译器抱怨的原因
第21行:
Cannot implicitly convert type 'UnityEngine.Object' to 'UnityEngine.Transform'. An explicit conversion exists (are you missing a cast?)
Cannot implicitly convert type 'UnityEngine.Vector3' to 'UnityEngine.Quaternion'
Operator * cannot be applied to operands of type 'UnityEngine.Quaternion' and 'float'
您正试图将对象
分配给转换
变量,虽然编译器无法自行处理它,但它已经在提示您什么可能是正确的解决方案
始终返回一个对象作为结果,因此您必须使用关键字as
将其转换回预制件的类型。在这种情况下,您正在克隆一个变换
对象,因此正确的代码为:
Transform canon = Instantiate(cannonPrefab, transform.position, Quaternion.identity) as Transform;
第22行:
Cannot implicitly convert type 'UnityEngine.Object' to 'UnityEngine.Transform'. An explicit conversion exists (are you missing a cast?)
Cannot implicitly convert type 'UnityEngine.Vector3' to 'UnityEngine.Quaternion'
Operator * cannot be applied to operands of type 'UnityEngine.Quaternion' and 'float'
现在您正试图将Vector3
分配给Quaternion
变量,而编译器也无法处理此问题
如果使用Vector3
创建一个Quaternion
,则会得到一个Vector3
,因此只需将shootDir
更改为Vector3
Vector3 shootDir = Quaternion.Euler(0, 0, angle) * Vector3.right;
第23行:
Cannot implicitly convert type 'UnityEngine.Object' to 'UnityEngine.Transform'. An explicit conversion exists (are you missing a cast?)
Cannot implicitly convert type 'UnityEngine.Vector3' to 'UnityEngine.Quaternion'
Operator * cannot be applied to operands of type 'UnityEngine.Quaternion' and 'float'
你不能用一个float
乘一个Quaternion
,但是因为shootDir
现在是一个Vector3
乘一个float
不再是个问题。谢谢你的回答,我想我已经得到了答案,我所做的是,使用javascript,我使用typeof来识别shootDir和p的数据类型打印它。之后我发现它是一个向量3。然后我在我的C#脚本中将数据类型从四元数更改为向量3,它已经工作了!谢谢伙计。