物体朝向目标(u3d)

以下脚本均来自siki学院的教学视频,个人感觉非常有用,记录下来留着以后温习

Enemy朝向Player并向Player方向移动(2D拾荒者[敌人脚本]):

public class Enemy : MonoBehaviour
{

    private Vector2 targetPosition;
    private Transform player;
    private Rigidbody2D rigidbody;

    public float smoothing = 3;
    public int lossFood = 10;
    public AudioClip attackAudio;

    private BoxCollider2D collider;
    private Animator animator;

    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        rigidbody = GetComponent<Rigidbody2D>();
        collider = GetComponent<BoxCollider2D>();
        animator = GetComponent<Animator>();
        targetPosition = transform.position;
        GameManager.Instance.enemyList.Add(this);
    }

    void Update()
    {
        rigidbody.MovePosition( Vector2.Lerp(transform.position, targetPosition, smoothing * Time.deltaTime));
    }

    public void Move()
    {
        Vector2 offset = player.position - transform.position;//主角位置减当前位置
        if (offset.magnitude < 1.1f)//偏移的大小
        {
            //攻击(Enemy和Player位置相邻时Enemy可以攻击Player)
            animator.SetTrigger("Attack");
            AudioManager.Instance.RandomPlay(attackAudio);
            player.SendMessage("TakeDamage",lossFood);
        }
        else
        {
            float x= 0,y=0;
            if (Mathf.Abs(offset.y) > Mathf.Abs(offset.x))
            {
                //按照y轴移动
                if (offset.y < 0)
                {
                    y = -1;
                }
                else//y的正轴
                {
                    y = 1;
                }
            }
            else
            {
                //按照x轴移动
                if (offset.x > 0)
                {
                    x = 1;
                }
                else
                {
                    x = -1;
                }
            }
            //设置目标位置之前先做检测(以下内容为检测Enemy前方是否有障碍物)
            collider.enabled = false;
            RaycastHit2D hit = Physics2D.Linecast(targetPosition, targetPosition + new Vector2(x,y));
            collider.enabled = true;
            if (hit.transform == null)//前方没有任何物体或前方是食物时才能移动
            {
                targetPosition += new Vector2(x, y);
            }
            else
            {
                if (hit.collider.tag == "Food" || hit.collider.tag == "Suda")
                {
                    targetPosition += new Vector2(x, y);
                }
            }
        }
    }
}

Bullet追踪Enemy(塔防游戏[子弹脚本]):

private Transform target;//定义传送目标

public void SetTarget(Transform _target)
    {
		this.target = _target;
    }

void Update()
    {
		transform.LookAt(target.position);
		transform.Translate(Vector3.forward * speed * Time.deltaTime);
    }

记得设定子弹攻击的目标

目前只使用过这两种脚本,如果再用应用到的物体跟踪目标脚本会继续补上,也欢迎大佬们补充

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值