如何实现2D人物换枪

 换枪和移动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move: MonoBehaviour
{
    public GameObject [] gun;//储存所有枪
    new private Rigidbody2D rigidbody;
    private Vector2 input;//人物移动  
    private Vector2 mousePso; 
    public float Speed;
    private int gunNum; //记录枪得编号
    private Animator animantor;


    void  Start()
    {
        animantor = GetComponent<Animator>();
        rigidbody = GetComponent<Rigidbody2D>();
        gun[0].SetActive(true);   
    }
    void Update()
    {
        SwitchGun();
        input.x = Input.GetAxisRaw("Horizontal");
        input.y = Input.GetAxisRaw("Vertical");

        rigidbody.velocity = input.normalized * Speed;
        mousePso = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        if (mousePso.x > transform.position.x)
        {
            transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
        }
        else 
        {
            transform.rotation = Quaternion.Euler(new Vector3(0, 180, 0));
        }
        if (input != Vector2.zero)
        {
            animantor.SetBool("Ismoving", true);
        }
        else 
        {
            animantor.SetBool("Ismoving", false);
        }
    }
    void SwitchGun() 
    {
        if (Input.GetKeyDown(KeyCode.Q)) 
        {
            gun[gunNum].SetActive(false);
            if (--gunNum < 0) 
            {
                gunNum = gun.Length - 1;
            }
            gun[gunNum].SetActive(true);
        }
        if (Input.GetKeyDown(KeyCode.E))
        {
            gun[gunNum].SetActive(false);
            if (++gunNum >gun.Length-1)
            {
                gunNum = 0;
            }
            gun[gunNum].SetActive(true);
        }
    }
}

射击

public class Gun : MonoBehaviour
{
    public float interval;//开枪间隔

    public GameObject bulletPrefab;//子弹的预制体

    public GameObject shellPrefab;//弹壳的预制体

    protected Transform shelllPos;//弹仓位置
    protected Transform bulletPos;//子弹的位置

    protected Animator animator;

    protected Vector2 mousePos;//鼠标位置
    protected Vector2 direction;//鼠标的方向

    protected float timer;
    protected float FlipY;

    protected virtual void Start()
    {
        animator = GetComponent<Animator>();
        bulletPos = transform.Find("Muzzle");
        shelllPos = transform.Find("BulletShell");
        FlipY = transform.localScale.y;
        //Debug.Log(FlipY);
    }
    protected virtual void Update()
    {
        mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        if (mousePos.x < transform.position.x)
        {
            transform.localScale = new Vector3(FlipY, -FlipY, 1);
        }
        else
        {
            transform.localScale = new Vector3(FlipY, FlipY, 1);
        }
        Shoot();
        
    }

   protected virtual void Shoot()
    {
        direction = (mousePos - new Vector2(transform.position.x, transform.position.y)).normalized;
        transform.right = direction;
        if (timer != 0)
        {
            timer -= Time.deltaTime;
            if (timer <= 0)
            {
                timer = 0;
            }
        }
        if (Input.GetMouseButtonDown(0))
        {

            if (timer == 0)
            {

                timer = interval;
                Fire();
            }
        }

    }

   protected virtual void Fire()
    {
        animator.SetTrigger("Shoot");

        //GameObject bullet = Instantiate(bulletPrefab, bulletPos.position,Quaternion.identity);
        GameObject bullet = objectPool.Instance.GetObject(bulletPrefab);
        bullet.transform.position = bulletPos.position;
       
        
        float angle = Random.Range(-5f, 5f);
        bullet.GetComponent<bullet>().SetSpeed(Quaternion.AngleAxis(angle, Vector3.forward) * direction);

        GameObject shell = objectPool.Instance.GetObject(shellPrefab);
        shell.transform.position = shelllPos.position;
        shell.transform.rotation = shelllPos.rotation;
        // Instantiate(shellPrefab, shelllPos.position,shelllPos.rotation); 
    }

}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值