如何在c#中向右侧的直线发射一个从枪中射出子弹的功能?

时间:2014-09-29 01:56:31

标签: 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 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);

}

我只是希望它从我的firePoint对象(在我的大炮口)画一条线(因为我还在学习)到Y轴上的+100个单位(范围= 100)。 / p>

我正在跟踪c#错误:

1)Assets / Gun.cs(47,69):错误CS1526:新类型表达式需要()或[]类型

2)Assets / Gun.cs(47,98):错误CS8032:解析期间内部编译器错误,使用-v运行以获取详细信息

有人能指出我正确的方向吗?

3 个答案:

答案 0 :(得分:2)

由于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);

这意味着你必须创建另一个Vector2来定义你的“射击线”的方向。

我附上了一个描述Raycast调用的链接 See the reference here

请尝试更新您的代码并返回

因为我可以看到你现在只想画线,你应该注释掉你的错误代码。所以你的代码应该是这样的:

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

答案 1 :(得分:1)

这条线对我来说似乎非常可疑:

  Vector2 target = new Vector2 ( firePoint.position.x
                               , [firePoint.position.y + Range]
                               );

你将一个看起来像阵列的东西传递给新功能,这就是为什么它会抱怨我相信。

你试着说:

  Vector2 target = new Vector2 ( firePoint.position.x 
                               , (firePoint.position.y + Range)
                               );

答案 2 :(得分:0)

回答解雇问题: 我假设您正在使用精灵和2D环境,因为您使用的是Vector2。如果是这种情况,您可能想要使用Vector2.rightTransform.forward来强制或直接更改“项目符号”变换,后者将根据角色所面对的位置而变化。 如果你要在firePointPosition变换中instantiate子弹预制件,我会建议只对预制件施加一个力来实例化。 类似的东西:

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