【发布时间】:2014-07-11 07:28:28
【问题描述】:
我有一个 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?)
这是 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# 代码
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;
}
}
}
我还是这个脚本的新手,还不熟悉数据类型。我希望你能帮助我。谢谢!
【问题讨论】:
标签: c# unity3d unityscript