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;
}
}
}
}
他们的顺序被打破了,我是一个初学者,请帮助。我不知道发生了什么事,请帮助。我写这些是因为它说我的帖子主要是代码.帮助pls thx.
这里的结尾缺少了paren。
if (hit.normal == new Vector3(0f, 0f, -1.0f) || hit.normal == new Vector3(-1.0f, 0f, 0f)
应该是
if (hit.normal == new Vector3(0f, 0f, -1.0f) || hit.normal == new Vector3(-1.0f, 0f, 0f))
有了:它应该会自行修复。
(附带说明:精确比较浮点数可能是不可取的)
这是你重构后的代码。不过我建议你自己试试。首先从开括号和收括号开始,如果你用的是VS,也可以做到ctrl k+d,这样你就可以看到格式化的代码了,方便你调试问题。
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;
}
}
}
}