C# 我的子弹的统一位置不';不能使用RayCastHit进行更改

C# 我的子弹的统一位置不';不能使用RayCastHit进行更改,c#,unity3d,unityscript,C#,Unity3d,Unityscript,我正试图找出我的代码出了什么问题。我只是按照教程创建了一个测试游戏,但不知为什么我的代码不起作用。我继续学习教程,但我想有些东西确实不起作用,因为他用Javascript编写的代码,而我用C#编写的代码。当然变量也有一些变化。以下是我目前的代码: using UnityEngine; using System.Collections; public class Ammo : MonoBehaviour { public float damage; public float s

我正试图找出我的代码出了什么问题。我只是按照教程创建了一个测试游戏,但不知为什么我的代码不起作用。我继续学习教程,但我想有些东西确实不起作用,因为他用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等,但仍然没有改变任何东西。希望有人能在这方面帮助我,因为如果我的代码有问题,我甚至找不到日志。

本教程没有在内部if语句中使用大括号。
sparks.position
的赋值与标记无关

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;
}
如果省略括号,则“if”仅适用于一个语句,则这是个人风格偏好的问题。如果总是使用大括号,则可以更清楚地看到if语句的结尾

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;
} 

到底是什么不起作用?代码中的任何内容都不会改变
弹药的位置。你的意思是火花没有产生吗?您是否进行过任何调试以验证调用了
if(Physics.Raycast…
下的代码。是否将
hit
对象标记为
播放器
?虽然我还没有发布答案,但我已经得到了答案,因此感谢您的回答。:)