Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 朝玩家方向开枪_C#_Unity3d_Unity5 - Fatal编程技术网

C# 朝玩家方向开枪

C# 朝玩家方向开枪,c#,unity3d,unity5,C#,Unity3d,Unity5,我试图朝玩家方向开枪,但子弹已固定,但不要离开初始位置,不确定原因,代码如下: using UnityEngine; using System.Collections; public class AtackPlayer : MonoBehaviour { public string playerTag = "Player"; public AnimationClip startAtack; public AnimationClip endAtack; publi

我试图朝玩家方向开枪,但子弹已固定,但不要离开初始位置,不确定原因,代码如下:

using UnityEngine;
using System.Collections;

public class AtackPlayer : MonoBehaviour {
    public string playerTag = "Player";
    public AnimationClip startAtack;
    public AnimationClip endAtack;
    public float atackInterval = 2f;

    public GameObject enemyBullet;
    public float bulletSpeed = 20f;

    private Animator _anim;
    private Transform _transform;

    // Use this for initialization
    void Start () {
        _anim = GetComponentInParent<Animator>();
        _transform = GetComponent<Transform>();
    }
    private IEnumerator Atack(Vector2 playerPosition)
    {
        _anim.Play(startAtack.name);
        yield return new WaitForSeconds(startAtack.length); // executa o clipe e ataca
        GameObject thisBullet = Instantiate(enemyBullet, _transform.position, Quaternion.identity) as GameObject; //instancia o bullet prefab
        thisBullet.transform.position = Vector2.Lerp(thisBullet.transform.position, playerPosition, bulletSpeed * Time.deltaTime);
        _anim.Play(endAtack.name);
        yield return new WaitForSeconds(endAtack.length); // executa o clipe de finalização do ataque
        yield return new WaitForSeconds(atackInterval); // executa o clipe de finalização do ataque
    }
    // Update is called once per frame
    void Update () {


    }
    void OnTriggerEnter2D(Collider2D player)
    {
        if (player.gameObject.tag == playerTag)
        {
            Vector2 playerPos = player.transform.position;
            StartCoroutine(Atack(playerPos));
        }
    }
}
使用UnityEngine;
使用系统集合;
公共类AtackPlayer:MonoBehavior{
公共字符串播放器tag=“Player”;
公共动画剪辑startAtack;
公共动画剪辑endAtack;
公共浮动时间间隔=2f;
公共游戏对象enemyBullet;
公共浮球速度=20f;
私人动画师;
私有变换_变换;
//用于初始化
无效开始(){
_anim=getComponentParent();
_transform=GetComponent();
}
专用IEnumerator Atack(矢量2播放器位置)
{
_动漫游戏(startAtack.name);
yield返回新的WaitForSeconds(startAtack.length);//executa o clipe e ataca
GameObject thisBullet=实例化(enemyBullet,_transform.position,Quaternion.identity)为GameObject;//实例化o bullet预置
thisbill.transform.position=Vector2.Lerp(thisbill.transform.position,playerPosition,bulletSpeed*Time.deltaTime);
_动漫游戏(endAtack.name);
返回新的WaitForSeconds(endAtack.length);//executa o clipe de finalizaaão do ataque
返回新的WaitForSeconds(atackInterval);//执行最后定稿
}
//每帧调用一次更新
无效更新(){
}
无效OnTriggerEnter2D(碰撞的R2D播放器)
{
如果(player.gameObject.tag==playerTag)
{
Vector2 playerPos=player.transform.position;
开始例行程序(Atack(playerPos));
}
}
}
bullet预置有一个rigidbody2d和一个圆碰撞器,还有一个精灵渲染器和一个动画师来处理它的动画

有什么帮助吗

thisBullet.transform.position = Vector2.Lerp(thisBullet.transform.position, playerPosition, bulletSpeed * Time.deltaTime);
t=bulletSpeed*Time.deltaTime
当t=0时,返回a。当t=1时返回b。当t=0.5时,返回a和b之间的中点

在游戏中,t的值可能非常接近于0。这意味着你的 子弹的位置几乎保持不变。你还需要一个 循环Atack函数,使项目符号与 子弹


你可以在这里设置你想要的任何游戏的while条件。在这段代码中,它每0.1f移动一次到玩家的位置。

也许子弹相信和平、爱和理解。我想它会杀死他们所有人=(我想这是因为你从来没有告诉过它离开初始位置。初始位置在一行:thisbillot.transform.position=Vector2.Lerp(thisbill.transform.position、playerPosition、bulletSpeed*Time.deltaTime);WaitForSeconds创建一个新对象以启动一个新的协同路由,请改用null,以便保存一些GC调用。
while (thisBullet.transform.position != playerPosition)
{    
    thisBullet.transform.position = Vector2.Lerp(thisBullet.transform.position, playerPosition, bulletSpeed * Time.deltaTime);
    yield return new WaitForSeconds(0.1f);
}