[英]UnityScript to C# code
I have a code in UnityScript and I want to convert it in C# but this is what happened. 我在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 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?)
this was the code for UnityScript 这是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;
}
}
and this was the C# code 这是C#代码
using UnityEngine;
using System.Collections;
public class projectileShot2 : MonoBehaviour {
public Transform cannonPrefab;
public float speed = 0f;
public float angle = 0f;
public float time = 0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1")){
Transform canon = Instantiate(cannonPrefab, transform.position, Quaternion.identity);
Quaternion shootDir = Quaternion.Euler(0, 0, angle) * Vector3.right;
canon.rigidbody.velocity = shootDir * speed;
}
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;
}
}
}
Im still a newbie with this scripting and still not familiar with the data types. 我仍然是使用此脚本的新手,并且仍然不熟悉数据类型。 I hope you can help me. 我希望你能帮助我。 Thanks! 谢谢!
I've run into this scenario multiple times at work, and have made heavy use of ILSpy . 我在工作中多次遇到这种情况,并大量使用了ILSpy 。 You let it compile to an assembly (the Library/ScriptAssemblies/Assembly-*.dll files), then use ILSpy to read and decompile the result to the language of your choice (like C#). 您可以将其编译为程序集(Library / ScriptAssemblies / Assembly-*。dll文件),然后使用ILSpy读取结果并将其反编译为您选择的语言(如C#)。
It has a few bugs and quirks that you still have to overcome, but since it does all the heavy lifting for you, there's minimal amount that you have to hand-tweak to fit with the rest of your code. 它仍然需要克服一些错误和怪癖,但是由于它为您完成了所有繁重的工作,因此您只需进行少量调整即可适应其余代码。
EDIT 1: 编辑1:
On line 23 , you have: 在第23行上,您具有:
canon.rigidbody.velocity = shootDir * speed;
shootDir
is a Quaternian, and while you can multiply by a Vector3
(more explanation about that here ), you cannot multiply by speed
. shootDir
是一个四元数,虽然可以乘以Vector3
( 在此进行更多说明),但不能乘以speed
。 Try speed * Vector3.forward
or some other direction. 尝试speed * Vector3.forward
或其他方向。
EDIT 2: 编辑2:
On line 21 , you need to change it from Instantiate(cannonPrefab,
to Instantiate(cannonPrefab.gameObject,
在第21行,您需要将其从Instantiate(cannonPrefab,
更改为Instantiate(cannonPrefab.gameObject,
EDIT 3: 编辑3:
I'm not entirely sure about line 22 , but I wonder what would happen if you wrapped everything right of the =
sign in parentheses, and whether that would get rid of that error/warning. 我不确定第22行,但我想知道如果将所有=
符号都放在括号中会发生什么情况,以及这是否会消除该错误/警告,该怎么办?
If you're having trouble with the correct data types, you can always check the Unity Scripting Reference . 如果您在使用正确的数据类型时遇到麻烦,可以随时查看Unity脚本参考 。 I linked the corresponding sites in my answers below. 我在下面的答案中链接了相应的网站。 Let's check every error and why the compiler is complaining. 让我们检查每个错误以及编译器抱怨的原因。
Line 21: 第21行:
Cannot implicitly convert type 'UnityEngine.Object' to 'UnityEngine.Transform'. An explicit conversion exists (are you missing a cast?)
You are trying to assign an Object
to a Transform
variable and while the compiler cannot handle it by itself, it is already giving you a hint what might be the correct solution. 您正在尝试将Object
分配给Transform
变量,而编译器无法单独处理它,但它已经在提示您什么是正确的解决方案。
Instantiate
always returns an Object
as result, so you have to cast it back to the type of your prefab using the keyword as
. Instantiate
始终返回一个Object
作为结果,因此您必须使用关键字as
将其强制转换回预制的类型。 In this case you're cloning a Transform
object, so the correct code would be: 在这种情况下,您正在克隆一个Transform
对象,因此正确的代码将是:
Transform canon = Instantiate(cannonPrefab, transform.position, Quaternion.identity) as Transform;
Line 22: 第22行:
Cannot implicitly convert type 'UnityEngine.Vector3' to 'UnityEngine.Quaternion'
Now you're trying to assign a Vector3
to a Quaternion
variable and again there is no way for the compiler to handle this. 现在,您正在尝试将Vector3
分配给Quaternion
变量,并且编译器也无法处理该变量。
If you multiply a Quaternion
with a Vector3
you get a Vector3
as a result, so you simply have to change shootDir
to a Vector3
如果将 Quaternion
与Vector3
相乘,您将得到一个Vector3
,因此,您只需要将shootDir
更改为Vector3
Vector3 shootDir = Quaternion.Euler(0, 0, angle) * Vector3.right;
Line 23: 第23行:
Operator * cannot be applied to operands of type 'UnityEngine.Quaternion' and 'float'
You cannot multiply a Quaternion
with a float
but because shootDir
is now a Vector3
the multiplication with a float
is no longer a problem. 您不能将Quaternion
与float
相乘,但是由于shootDir
现在是一个Vector3
,所以不再需要用float
相乘。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.