using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
//属性
public float MoveSpeed = 1;
private Vector3 BllectEulerAngles;
private float AttackTimeval = 0;//攻击间隔计时器
private float MoveTimeVal = 5;//移动间隔计时器
private float h;
private float v;
//引用
private SpriteRenderer sr;
public Sprite[] TankSprites;//上右下左
public GameObject Bllect;//子弹
public GameObject explasion;//死亡特效
void Start()
{
sr = GetComponent<SpriteRenderer>();
}
void Update()
{
if (AttackTimeval >= 3)
{
EnemyAttack();
}
else
{
AttackTimeval += Time.deltaTime;
}
}
void FixedUpdate()
{
if (MoveTimeVal >= 4)
{
EnemyMove();
MoveTimeVal = 0;
}
else
{
MoveTimeVal += Time.fixedDeltaTime;
}
}
private void EnemyMove()
{
int num = Random.Range(0, 8);
if (num >= 5)//下
{
h = 0;
v = -1;
}
else if (num == 0)//上
{
h = 0;
v = 1;
}
else if (num > 0 && num <= 2)//左
{
h = -1;
v = 0;
}
else if (num > 2 && num <= 4)//右
{
h = 1;
v = 0;
}
if (h != 0)//设置优先级
{
v = 0;
}
//移动
transform.Translate(Vector3.right * h * MoveSpeed*Time.fixedDeltaTime ,Space.World);
//变换图片
if (h < 0)//左
{
sr.sprite = TankSprites[3];
//设置子弹朝向
BllectEulerAngles = new Vector3(0, 0, 90);
}
else if (h > 0)//右
{
sr.sprite = TankSprites[1];
BllectEulerAngles = new Vector3(0, 0, -90);
}
//移动
transform.Translate(Vector3.up * v * MoveSpeed*Time.fixedDeltaTime ,Space.World);
//变换图片
if (v < 0)//下
{
sr.sprite = TankSprites[2];
BllectEulerAngles = new Vector3(0, 0, 180);
}
else if (v > 0)//上
{
sr.sprite = TankSprites[0];
BllectEulerAngles = new Vector3(0, 0, 0);
}
}
private void EnemyAttack()
{
//实例化子弹 旋转角度:当前坦克角度+子弹应旋转角度
Instantiate(Bllect, this.transform.position, Quaternion.Euler(transform.eulerAngles + BllectEulerAngles));
AttackTimeval = 0;
}
private void EnemyDie()
{
Instantiate(explasion, transform.position, Quaternion.identity);
Destroy(this.gameObject);
}
private void OnCollisionEnter2D(Collision collision)
{
if (collision.collider.tag.Equals("Enemy"))
{
MoveTimeVal = 4;
}
}
}
我按照视频中的来用translate方法控制Enemy移动,却移动的十分缓慢
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bllect : MonoBehaviour
{
// 属性
public float BllectSpeed = 10;
public bool IsPlayer = true;
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(transform.up * BllectSpeed * Time.deltaTime, Space.World);
}
//触发检测
void OnTriggerEnter2D(Collider2D collision)
{
switch (collision.tag)
{
case "Tank":
if (!IsPlayer)
{
collision.SendMessage("PlayerDie");
print("怎么肥四");
Destroy(collision.gameObject);
}
break;
case "Heart":
collision.SendMessage("HeartDie");
break;
case "Wall":
Destroy(collision.gameObject);
Destroy(gameObject);
break;
case "Barrier":
Destroy(gameObject);
break;
case "Enemy":
if (IsPlayer)
{
collision.SendMessage("EnemyDie");
Destroy(collision.gameObject);
Destroy(gameObject);
}
break;
default:
break;
}
}
}
为什么我的坦克被子弹击中时,触发检测了两次??