在等待一段时间后,攻击目标并且移动位置.
先上图(biu~biu~biu~)
完整代码
using UnityEngine;
using System.Collections.Generic;
namespace Assets.Scripts.ShootAndScoot
{
public class ShootAndScoot : MonoBehaviour
{
// 目标;
public Transform TargetTrans;
// 攻击距离;
public float AttackDistance;
// 等待时间;
public float WaitTime;
// 武器;
public GameObject wepon;
float waitTime = 0f;
void Update()
{
waitTime += Time.deltaTime;
if (waitTime > WaitTime)
{
waitTime = 0f;
// 开始攻击;
Attack();
}
}
void Attack()
{
Move();
Shoot();
}
void Move()
{
Vector3 targetPos = TargetTrans.localPosition;
Vector3 selfPos = transform.localPosition;
int random = Random.Range(-100, 100);
transform.localPosition = new Vector3(targetPos.x + random, selfPos.y, selfPos.z);
}
void Shoot()
{
wepon.transform.localPosition = Vector3.zero;
wepon.SetActive(true);
iTween.MoveTo(wepon, iTween.Hash("position", TargetTrans.position, "time", 0.5f, "oncomplete", "Hit"));
}
void Hit()
{
wepon.SetActive(false);
wepon.transform.localPosition = Vector3.zero;
}
}
}