游戏玩家的移动(坦克游戏)
程序员文章站
2022-03-31 20:47:22
...
public class player : MonoBehaviour {
//定义常量
public float movespeed=3;
private float TimeVal;
//定义引用
private SpriteRenderer sr;
public Sprite[] tankSprite;//上 右 下 左
private Vector3 bulletAularAngles;
//获取预制体
public GameObject m_bullet;
public GameObject m_baozha;
public GameObject m_chusheng;
private bool isdended=true;
private float bornTime = 3.0f;
public void Awake()
{
sr = GetComponent<SpriteRenderer>();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//出生受保护状态
if (isdended)
{
m_chusheng.SetActive(true);
bornTime -= Time.deltaTime;
if (bornTime <= 0)
{
isdended = false;
m_chusheng.SetActive(false);
}
}
}
public void FixedUpdate()
{
if (PlayerManger.Instance.isDef)
{
return;
}
MoveTank();
//CD冷却时间
if (TimeVal >= 0.4f)
{
AttackTank();
}
else
{
TimeVal += Time.fixedDeltaTime;
}
}
/// <summary>
/// 坦克攻击方法
/// </summary>
private void AttackTank()
{
if (Input.GetKeyDown(KeyCode.Space))
{
GameObject zd=Instantiate(m_bullet,transform.position,Quaternion.Euler(transform.eulerAngles+bulletAularAngles));
TimeVal = 0;
Destroy(zd, 3.0f);
}
}
/// <summary>
/// 坦克移动方法
/// </summary>
private void MoveTank()
{
float v = Input.GetAxisRaw("Vertical");
transform.Translate(Vector3.up * v * movespeed * Time.fixedDeltaTime, Space.World);
if (v < 0)
{
sr.sprite = tankSprite[2];
bulletAularAngles = new Vector3(0, 0, -180);
}
else if (v > 0)
{
sr.sprite = tankSprite[0];
bulletAularAngles = new Vector3(0, 0, 0);
}
if (v != 0)
{
return;
}
float h = Input.GetAxisRaw("Horizontal");
transform.Translate(Vector3.right * h * movespeed * Time.fixedDeltaTime, Space.World);
if (h < 0)
{
sr.sprite = tankSprite[3];
bulletAularAngles = new Vector3(0, 0, 90);
}
else if (h > 0)
{
sr.sprite = tankSprite[1];
bulletAularAngles = new Vector3(0, 0, -90);
}
}
/// <summary>
/// 玩家死亡方法
/// </summary>
private void death()
{
if (isdended)
{
return;
}
//玩家生命改变
PlayerManger.Instance.isDead = true;
//产生爆炸特效
GameObject baozha = Instantiate(m_baozha, transform.position, transform.rotation);
Destroy(baozha,0.167f);
//死亡
Destroy(gameObject);
}
}
上一篇: Unity单例模式基类模板