【发布时间】:2014-07-13 01:36:31
【问题描述】:
我正试图找出我的代码有什么问题。我只是按照教程here 创建一个测试游戏,但由于未知原因,我的代码无法正常工作。我按照教程进行操作,但我想有些东西真的不起作用,因为他所做的代码是用 Javascript 编写的,而我是用 C# 编写的。当然变量的一些变化。到目前为止,这是我的代码:
using UnityEngine;
using System.Collections;
public class Ammo : MonoBehaviour {
public float damage;
public float spread;
public float recoil;
public float weight;
public int clip_rounds;
public int ammo;
public Transform sparks;
public float fire_rate;
public shells shell = shells.AP_SHELL;
public enum shells {HEAT, AP_SHELL}
private int mag;
private float fire_delay = 0.0f;
public void FireA(){
RaycastHit hit;
if (Physics.Raycast (transform.position, transform.forward, out hit, Mathf.Infinity)) {
if(hit.transform.gameObject.tag=="Player"){
hit.transform.SendMessage("M_Damage", damage);
sparks.position = hit.point;
}
}
mag--;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
switch (shell) {
case shells.AP_SHELL:
if(Input.GetButton("Fire1") && Time.time > fire_delay){
fire_delay = Time.deltaTime + fire_rate;
FireA();
}
break;
case shells.HEAT:
if(Input.GetButtonUp("Fire1") && Time.time > fire_delay){
fire_delay = Time.deltaTime + fire_rate;
FireA();
}
break;
}
}
}
我怀疑 RayCastHit 是导致我的代码无法运行的原因,尽管我对此不太确定。我还尝试将 transform.forward 更改为 Vector3.forward 等,但仍然没有改变任何事情。希望有人可以帮助我,因为如果我的代码有问题,我什至找不到日志。
【问题讨论】:
-
究竟是什么不起作用?您的代码中的任何内容都不会改变
Ammo的位置。你的意思是火花不是被创造出来的吗?您是否进行了任何调试以验证if(Physics.Raycast...下的代码是否被调用。hit对象是否标记为Player?
标签: c# unity3d unityscript