简体   繁体   中英

UnityScript to C# code

I have a code in UnityScript and I want to convert it in C# but this is what happened.

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

#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

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 . 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#).

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.

On line , you have: 行上,您具有:

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 . Try speed * Vector3.forward or some other direction.

On line , you need to change it from Instantiate(cannonPrefab, to Instantiate(cannonPrefab.gameObject, 行,您需要将其从Instantiate(cannonPrefab,更改为Instantiate(cannonPrefab.gameObject,

I'm not entirely sure about line , 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. 行,但我想知道如果将所有=符号都放在括号中会发生什么情况,以及这是否会消除该错误/警告,该怎么办?

If you're having trouble with the correct data types, you can always check the Unity Scripting Reference . I linked the corresponding sites in my answers below. Let's check every error and why the compiler is complaining.

Line 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.

Instantiate always returns an Object as result, so you have to cast it back to the type of your prefab using the keyword as . In this case you're cloning a Transform object, so the correct code would be:

Transform canon = Instantiate(cannonPrefab, transform.position, Quaternion.identity) as Transform;

Line 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.

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

Vector3 shootDir = Quaternion.Euler(0, 0, angle) * Vector3.right;

Line 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM