unity3d C#写发射子弹的程序怎么写
我在自学网上看--他用的JAVAs,写了个#pragmastrictfunctionStart(){}varspeed:int=5;varnewobject:Transf...
我在自学网上 看- -他用的JAVAs,写了个
#pragma strict
function Start () {
}
var speed:int =5;
var newobject:Transform;
function Update () {
var x:float =Input.GetAxis("Horizontal")*Time.deltaTime*speed;
var z:float=Input.GetAxis("Vertical")*Time.deltaTime*speed;
transform.Translate(x,0,z);
print(x);
if (Input.GetButtonDown ("Fire1")) {
var n:Transform =Instantiate(newobject,transform.position,transform.rotation);
var fwd:Vector3 =transform.TransformDirection(Vector3.forward);
n.rigidbody.AddForce(fwd*20000);
}
}
- -然后我想把他转化为C#却错误
using UnityEngine;
using System.Collections;
public class ffc : MonoBehaviour {
// Use this for initialization
void Start () {
}
int speed=5;
Transform newobject;
// Update is called once per frame
void Update () {
float x = Input.GetAxis ("Horizontal") * Time.deltaTime * speed;
float z = Input.GetAxis ("Vertical") * Time.deltaTime * speed;
transform.Translate (x, 0, z);
if (Input.GetButtonDown ("Fire1")) {
Transform n=Instantiate(newobject,transform.position,transform.rotation);
Vector3 fwd=transform.TransformDirection(Vector3.forward);
n.rigidbody.AddForce(fwd*20000);
}
}
}
求解,他说的是 Transform n=Instantiate(newobject,transform.position,transform.rotation);这句不对,我应该怎么改呢? 展开
#pragma strict
function Start () {
}
var speed:int =5;
var newobject:Transform;
function Update () {
var x:float =Input.GetAxis("Horizontal")*Time.deltaTime*speed;
var z:float=Input.GetAxis("Vertical")*Time.deltaTime*speed;
transform.Translate(x,0,z);
print(x);
if (Input.GetButtonDown ("Fire1")) {
var n:Transform =Instantiate(newobject,transform.position,transform.rotation);
var fwd:Vector3 =transform.TransformDirection(Vector3.forward);
n.rigidbody.AddForce(fwd*20000);
}
}
- -然后我想把他转化为C#却错误
using UnityEngine;
using System.Collections;
public class ffc : MonoBehaviour {
// Use this for initialization
void Start () {
}
int speed=5;
Transform newobject;
// Update is called once per frame
void Update () {
float x = Input.GetAxis ("Horizontal") * Time.deltaTime * speed;
float z = Input.GetAxis ("Vertical") * Time.deltaTime * speed;
transform.Translate (x, 0, z);
if (Input.GetButtonDown ("Fire1")) {
Transform n=Instantiate(newobject,transform.position,transform.rotation);
Vector3 fwd=transform.TransformDirection(Vector3.forward);
n.rigidbody.AddForce(fwd*20000);
}
}
}
求解,他说的是 Transform n=Instantiate(newobject,transform.position,transform.rotation);这句不对,我应该怎么改呢? 展开
2个回答
展开全部
木有类型转换
GameObject n=Instantiate(newobject,transform.position,transform.rotation) as GameObject;
Transform nt=n.transform;
Js和C#不同,Instantiate返回的是Object类,需要类型转换
GameObject n=Instantiate(newobject,transform.position,transform.rotation) as GameObject;
Transform nt=n.transform;
Js和C#不同,Instantiate返回的是Object类,需要类型转换
追问
怎么转??
追答
如回答所说
GameObject n=Instantiate(newobject,transform.position,transform.rotation) as GameObject;
中结尾添加as GameObject
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
展开全部
Transform n = Instantiate(newobject,transform.position,transform.rotation) as Transform;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询