如何生成一个函数,在c#中以直线从枪中向右发射子弹?
第一:我真是个笨蛋 以下是我的射击功能代码:如何生成一个函数,在c#中以直线从枪中向右发射子弹?,c#,graphics,unity3d,C#,Graphics,Unity3d,第一:我真是个笨蛋 以下是我的射击功能代码: using UnityEngine; using System.Collections; public class Gun : MonoBehaviour { public float fireRate = 0; // makes it a single fire per click weapon public float Damage = 10; public LayerMask notToHit; // tells us what is va
using UnityEngine;
using System.Collections;
public class Gun : MonoBehaviour
{
public float fireRate = 0; // makes it a single fire per click weapon
public float Damage = 10;
public LayerMask notToHit; // tells us what is valid for being hit by the weapon
public float Range = 100;
float timeToFire = 0;
Transform firePoint;
// Use this for initialization
void Awake ()
{
firePoint = transform.FindChild ("firePoint");
if (firePoint == null)
{
Debug.LogError ("No FirePoint object found as a child of the cannon");
}
}
// Update is called once per frame
void Update ()
{
Shoot ();
if (fireRate == 0)
{
if (Input.GetButtonDown ("Fire1"))
{
Shoot ();
}
}
else
{
if (Input.GetButtonDown ("Fire1") && Time.time > timeToFire) {
timeToFire = Time.time + 1 / fireRate;
Shoot ();
}
}
}
void Shoot ()
{
Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
Vector2 target = new Vector2 (firePoint.position.x, [firePoint.position.y + Range]);
RaycastHit2D hit = Physics2D.Raycast (firePoint, notToHit);
Debug.DrawLine (firePointPosition, target);
}
我只想让它从我的火点对象(在我的大炮口)到Y轴上的+100个单位(射程=100)画一条线(因为我还在学习)
我发现以下c#错误:
1) Assets/Gun.cs(47,69):错误CS1526:新表达式需要在类型后面加上()或[]
2) Assets/Gun.cs(47,98):错误CS8032:解析过程中出现内部编译器错误,请使用-v运行以获取详细信息
有人能给我指出正确的方向吗?我觉得这条线很可疑:
Vector2 target = new Vector2 ( firePoint.position.x
, [firePoint.position.y + Range]
);
您正在将看起来像数组的东西传递给新函数,我相信这就是它抱怨的原因
你有没有试着说:
Vector2 target = new Vector2 ( firePoint.position.x
, (firePoint.position.y + Range)
);
因为Noctis已经写了Vector2目标行是错误的。应该是这样的
Vector2 target = new Vector2 ( firePoint.position.x, (firePoint.position.y + Range));
在这之后,您说您有另一个编译错误,但是第48行有一个错误,所以Raycast调用是错误的。
要调用Raycast,您必须执行以下操作:
RaycastHit2D hit = Physics2D.Raycast (Origin, Direction);
// So your code should be
RaycastHit2D hit = Physics2D.Raycast (firePoint, DIRECTION);
void Shoot ()
{
Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
Vector2 target = new Vector2 (firePoint.position.x, (firePoint.position.y + Range));
// No need to do the ray casting right now
// RaycastHit2D hit = Physics2D.Raycast (firePoint, notToHit);
Debug.DrawLine (firePointPosition, target);
}
这意味着您必须创建另一个矢量2,它定义了“拍摄线”的方向
我附加了一个链接,描述了对Raycast的呼叫
请尝试更新您的代码并返回
因为我知道你现在只想划清界限,你应该注释掉你的错误代码。因此,您的代码应该如下所示:
RaycastHit2D hit = Physics2D.Raycast (Origin, Direction);
// So your code should be
RaycastHit2D hit = Physics2D.Raycast (firePoint, DIRECTION);
void Shoot ()
{
Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
Vector2 target = new Vector2 (firePoint.position.x, (firePoint.position.y + Range));
// No need to do the ray casting right now
// RaycastHit2D hit = Physics2D.Raycast (firePoint, notToHit);
Debug.DrawLine (firePointPosition, target);
}
回答以下问题: 我假设您使用的是精灵和2D环境,因为您使用的是Vector2。如果是这种情况,您可能希望应用力或直接使用或甚至更改“bullet”变换,后者将根据角色面对的位置而改变。 如果你要在firePointPosition转换中使用子弹预置,我建议你只对预置应用一个力,然后实例化。 比如:
public GameObject bulletPrefab; //Initialize it either by drag&drop from the editor or through code
private float bulletSpeed; // self explanatory
/* Rest of the code*/
GameObject bullet = Instantiate(bulletPrefab, firePointPosition, Quaternion.identity) as GameObject;
bullet.rigidbody.AddForce(Vector2.right * bulletSpeed);
// or
bullet.rigidbody.AddForce(transform.forward * bulletSpeed);
对不起,就像我说的,我是新来的。我已经在原始帖子中发布了完整的脚本,具体是哪行造成了错误?它是Vector2目标=新的Vector2(firePoint.position.x[firePoint.position.y+范围]?如果是的话。如果删除范围并尝试编译代码会怎么样?Vector2目标=新的Vector2(firePoint.position.x,firePoint.position.y)?是的,这是我的第47Yes行:这将错误更改如下:1)Assets/Gun.cs(48,46):错误CS1502:UnityEngine.Physics2D.Raycast(UnityEngine.Vector2,UnityEngine.Vector2)的最佳重载方法匹配有一些无效参数,第二个错误:2)Assets/Gun.cs(48,46):error CS1503:Argument#1'无法将
UnityEngine.Transform'表达式转换为type
UnityEngine.Vector2'@PaulNowak基于您的错误,此答案实际上修复了第一个错误,但在我看来,您调用Raycast的操作似乎是错误的。嗯,好吧……我相信您,但我该怎么办?如果我可以介入这里,@PaulNowak,看来你想在走路之前先跑。你知道向量是什么吗?什么是光线投射?转换是如何工作的?老实说,不是真的。我正在学习一个教程,但是它是用来在鼠标指向的任何地方射击的。我只想让枪沿y轴沿直线发射,所以我试图修改它。如果你知道这样发射武器的教程,我很乐意观看并重新开始:)非常感谢!现在我可以开始弄清楚这是怎么回事:)没问题。如果您对答案感到满意,则应将其标记为“已回答”。祝您的项目好运:)