Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.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_2d - Fatal编程技术网

C# 如何使子弹从玩家所在的方向发起

C# 如何使子弹从玩家所在的方向发起,c#,unity3d,2d,C#,Unity3d,2d,我正在统一制作2D游戏 在这篇文章中,我想添加一个有限次数的子弹 子弹射向球员所在的方向,但始终从右侧发射,即使球员面向左侧。我把子弹数限制在3颗 我如何在子弹出现之间设置延迟 第一稿(项目符号) 公共类项目符号:单一行为{ 私人玩家; 公共浮子速度=1f; 公共int abc=2; //用于初始化 无效开始(){ player=GameObject.Find(“player”).GetComponent(); if(player.aa.x==transform.localScale.x) ab

我正在统一制作2D游戏

在这篇文章中,我想添加一个有限次数的子弹

子弹射向球员所在的方向,但始终从右侧发射,即使球员面向左侧。我把子弹数限制在3颗

我如何在子弹出现之间设置延迟

第一稿(项目符号)

公共类项目符号:单一行为{
私人玩家;
公共浮子速度=1f;
公共int abc=2;
//用于初始化
无效开始(){
player=GameObject.Find(“player”).GetComponent();
if(player.aa.x==transform.localScale.x)
abc=1;
}
//每帧调用一次更新
公共无效更新(){
如果(abc==1)
rigidbody2D.velocity=新矢量3(transform.localScale.x,0,1)*速度;
其他的
rigidbody2D.velocity=新矢量3(transform.localScale.x,0,1)*速度;
}
}
第二脚本(玩家)

公共类玩家:单行为{
公共浮子速度=10f;
公共向量2 maxVelocity=新向量2(3,5);
公众利益;
公共浮子喷射速度=15f;
公共浮子空速倍增管=.3f;
公共音频剪辑leftFootSound;
公共音频剪辑rightFootSound;
公共音频剪辑重击声;
公共音频剪辑rocketSound;
公共向量3 aa=新向量3(1,1,1);
私人动画师;
专用播放控制器;
void Start(){
controller=GetComponent();
animator=GetComponent();
}
void PlayLeftFootSound(){
if(左足音)
AudioSource.PlayClipAtPoint(leftFootSound,transform.position);
}
void PlayRightFootSound(){
if(右脚声)
AudioSource.PlayClipAtPoint(右脚声音、变换、位置);
}
void PlayRocketSound(){
如果(!rocketSound | | GameObject.Find(“rocketSound”))
回来
GameObject go=新游戏对象(“RocketSound”);
AudioSource aSrc=go.AddComponent();
aSrc.clip=rocketSound;
aSrc.体积=0.7f;
Play();
破坏(前进,火箭声,长度);
}
无效OnCollisionInter2D(碰撞2D目标){
如果(!站着){
var absVelX=Mathf.Abs(刚体2d.velocity.x);
var Absvly=Mathf.Abs(刚体2d.velocity.y);
if(absVelX 0){
PlayRocketSound();
if(绝对<最大速度y)
forceY=jetSpeed*controller.moving.y;
animator.SetInteger(“AnimState”,2);
}否则如果(绝对值>0){
animator.SetInteger(“AnimState”,3);
}
刚体2d.AddForce(新向量2(forceX,forceY));
}
}
第三脚本(PlayerController)

公共类PlayerController:MonoBehavior{
公共向量2移动=新向量2();
公共限额=0;
公共int MaxBulletlimit=3;
公共交通延误=3f;
公共枪械;
公众子弹;
//用于初始化
void Start(){}
//每帧调用一次更新
无效更新(){
移动。x=移动。y=0;
if(Input.GetKey(“right”)){
移动,x=1;
}else if(Input.GetKey(“左”)){
移动。x=-1;
}
if(Input.GetKey(“up”)){
移动,y=1;
}else if(Input.GetKey(“down”)){
移动。y=-1;
}
if(Input.GetKey(“s”)){
如果(枪){
if(Bulletlimit
1)有一种简单的方法可以知道子弹的位置和射击方向。在角色下方添加一个子虚拟游戏对象,作为子弹的初始位置。将其放置在您想要的位置。现在此游戏对象相对于角色移动和旋转。使用它的
变换。位置
实例化项目符号时,transform.rotation.forward


2) 保持用户在变量中发射子弹时的当前时间,如
private float lastShotTime;
。在发射子弹时更新其值
lastShotTime=time.time
。然后当用户想要发射另一颗子弹时,检查自上次发射以来是否经过了足够的时间
如果(time.time>lastShotTime+fireDelay){shot();}

我正确地得到了bullet delay,但是得到了一个参数异常,即get_time只能从主线程调用,在位置1中,我无法找到玩家面对的当前面,并通过将time.time放在函数中来移除异常,但不知道为什么需要这样做.
public class Bullet : MonoBehaviour {

    private Player player;
    public float speed = 1f;
    public int abc = 2;

    // Use this for initialization
    void Start () {
        player = GameObject.Find ("Player").GetComponent<Player> ();
        if (player.aa.x == transform.localScale.x)
            abc = 1;
    }

    // Update is called once per frame
    public void Update () {
        if (abc == 1)
            rigidbody2D.velocity = new Vector3 (transform.localScale.x, 0, 1) * speed;
        else
            rigidbody2D.velocity = new Vector3 (transform.localScale.x, 0, 1) * speed;
    }
}
public class Player : MonoBehaviour {

    public float speed = 10f;
    public Vector2 maxVelocity = new Vector2(3, 5);
    public bool standing;
    public float jetSpeed = 15f;
    public float airSpeedMultiplier = .3f;
    public AudioClip leftFootSound;
    public AudioClip rightFootSound;
    public AudioClip thudSound;
    public AudioClip rocketSound;
    public Vector3 aa = new Vector3(1,1,1);

    private Animator animator;
    private PlayerController controller;

    void Start(){
        controller = GetComponent<PlayerController> ();
        animator = GetComponent<Animator> ();
    }

    void PlayLeftFootSound(){
        if (leftFootSound)
            AudioSource.PlayClipAtPoint (leftFootSound, transform.position);
    }

    void PlayRightFootSound(){
        if (rightFootSound)
            AudioSource.PlayClipAtPoint (rightFootSound, transform.position);
    }

    void PlayRocketSound(){
        if (!rocketSound || GameObject.Find ("RocketSound"))
            return;

        GameObject go = new GameObject ("RocketSound");
        AudioSource aSrc = go.AddComponent<AudioSource> ();
        aSrc.clip = rocketSound;
        aSrc.volume = 0.7f;
        aSrc.Play ();

        Destroy (go, rocketSound.length);
    }

    void OnCollisionEnter2D(Collision2D target){
        if (!standing) {
            var absVelX = Mathf.Abs(rigidbody2D.velocity.x);
            var absVelY = Mathf.Abs(rigidbody2D.velocity.y);

            if(absVelX <= .1f || absVelY <= .1f){
                if(thudSound)
                    AudioSource.PlayClipAtPoint(thudSound, transform.position);
            }
        }
    }

    // Update is called once per frame
    void Update () {
        var forceX = 0f;
        var forceY = 0f;

        var absVelX = Mathf.Abs (rigidbody2D.velocity.x);
        var absVelY = Mathf.Abs (rigidbody2D.velocity.y);

        if (absVelY < .2f)
            standing = true;
        else
            standing = false;

        if (controller.moving.x != 0) {
            if (absVelX < maxVelocity.x) {

                forceX = standing ? speed * controller.moving.x : (speed * controller.moving.x * airSpeedMultiplier);

                aa = transform.localScale = new Vector3 (forceX > 0 ? 1 : -1, 1, 1);

            }

            animator.SetInteger ("AnimState", 1);

        } else {
            animator.SetInteger ("AnimState", 0);
        }

        if (controller.moving.y > 0) {
            PlayRocketSound();
                        if (absVelY < maxVelocity.y)
                                forceY = jetSpeed * controller.moving.y;

                        animator.SetInteger ("AnimState", 2);
                } else if (absVelY > 0) {
            animator.SetInteger("AnimState", 3);
                }

        rigidbody2D.AddForce (new Vector2 (forceX, forceY));
    }
}
public class PlayerController : MonoBehaviour {

    public Vector2 moving = new Vector2();
    public int Bulletlimit = 0;
    public int MaxBulletlimit = 3;
    public float bulletDelay = 3f;
    public bool Gun;

    public Bullet bullet;
    // Use this for initialization
    void Start () {}

    // Update is called once per frame
    void Update () {

        moving.x = moving.y = 0;

        if (Input.GetKey ("right")) {
            moving.x = 1;
        } else if (Input.GetKey ("left")) {
            moving.x = -1;
        }

        if (Input.GetKey ("up")) {
            moving.y = 1;
        } else if (Input.GetKey ("down")) {
            moving.y = -1;
        }

        if (Input.GetKey ("s")) {
            if(Gun){
                if(Bulletlimit < MaxBulletlimit)
                {
                    Bullet clone = Instantiate (bullet, transform.position, Quaternion.identity) as Bullet;
                    Bulletlimit = Bulletlimit + 1;
                }
            }
        }   
    }

    public void BulletCount() {
        Bulletlimit = Bulletlimit - 1;
    }
}