换枪和移动
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);
}
}