C# 花括号断裂

C# 花括号断裂,c#,unity3d,C#,Unity3d,他们的顺序是打破我是一个初学者请帮助。我不知道发生了什么,请帮忙。我之所以写这些,是因为上面说我的帖子主要是代码。请帮忙,谢谢。此处末尾缺少参数: using System; using System.Runtime.InteropServices.ComTypes; using UnityEngine; public class GunScript : MonoBehaviour { public float damage = 10f; public float fireRa

他们的顺序是打破我是一个初学者请帮助。我不知道发生了什么,请帮忙。我之所以写这些,是因为上面说我的帖子主要是代码。请帮忙,谢谢。

此处末尾缺少参数:

using System;
using System.Runtime.InteropServices.ComTypes;
using UnityEngine;

public class GunScript : MonoBehaviour
{
    public float damage = 10f;
    public float fireRate = 1f;
    public float range = 100f;

    private float nextTimeToFire = 0f;

    public Camera MainCam;
    public ParticleSystem muzzleFlash;
    public GameObject impactEffect;
    public GameObject bulletDecal;

    void Update()
    {
        if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
        {
            nextTimeToFire = Time.time + 1f / fireRate;
            Shoot();
        }
    }

    void Shoot()
    {
        muzzleFlash.Play();
        RaycastHit hit;

        if (Physics.Raycast(MainCam.transform.position, MainCam.transform.forward, out hit, range))
        {
            Target target = hit.transform.GetComponent<Target>();

            if (target != null)
            {
                target.TakeDamage(damage);
            }

            GameObject ImpactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
            GameObject bulletDecalGO = Instantiate(bulletDecal);

            Vector3 zFighting = new Vector3(-0.001f, 0f, -0.001f);
            Vector3 zFighting2 = new Vector3(0.001f, 0f, 0.001f);

            if (hit.normal == new Vector3(0f, 0f, -1.0f) || hit.normal == new Vector3(-1.0f, 0f, 0f)
            {
                bulletDecalGO.transform.position = zFighting + hit.point;
            }
        }
    }
}
应该是

if (hit.normal == new Vector3(0f, 0f, -1.0f) || hit.normal == new Vector3(-1.0f, 0f, 0f)
因此:它应该自己修复


作为旁注:精确比较浮点可能是不可取的

这是您的重构代码。但我建议你自己试试。首先从开始和结束括号开始。如果您使用的是VS,那么您也可以执行ctrl k+d,这样您就可以看到格式化的代码,这样您就可以很容易地调试问题

if (hit.normal == new Vector3(0f, 0f, -1.0f) || hit.normal == new Vector3(-1.0f, 0f, 0f))

非常感谢你!我会接受你的回答。如果点击,你在行中缺少一个结束括号。正常…当你将光标放在开始的卷曲上时,结束括号将高亮显示。
using System;
using System.Runtime.InteropServices.ComTypes;
using UnityEngine;

public class GunScript : MonoBehaviour
{
    public float damage = 10f;
    public float fireRate = 1f;
    public float range = 100f;

    private float nextTimeToFire = 0f;

    public Camera MainCam;
    public ParticleSystem muzzleFlash;
    public GameObject impactEffect;
    public GameObject bulletDecal;

    void Update()
    {
        if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
        {
            nextTimeToFire = Time.time + 1f / fireRate;
            Shoot();
        }
    }

    void Shoot()
    {
        muzzleFlash.Play();
        RaycastHit hit;

        if (Physics.Raycast(MainCam.transform.position, MainCam.transform.forward, out hit, range))
        {
            Target target = hit.transform.GetComponent<Target>();

            if (target != null)
            {
                target.TakeDamage(damage);
            }

            GameObject ImpactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
            GameObject bulletDecalGO = Instantiate(bulletDecal);

            Vector3 zFighting = new Vector3(-0.001f, 0f, -0.001f);
            Vector3 zFighting2 = new Vector3(0.001f, 0f, 0.001f);

            if (hit.normal == new Vector3(0f, 0f, -1.0f) || hit.normal == new Vector3(-1.0f, 0f, 0f))
            {
                bulletDecalGO.transform.position = zFighting + hit.point;
            }
        }
    }
}