赞
踩
代码部分:
- using UnityEngine;
-
- public class enemy_1 : MonoBehaviour
- {
- Rigidbody2D rig;
- int dir = 1;
- Transform front;
- void Start()
- {
- rig = GetComponent<Rigidbody2D>();
- front = transform.Find("front");
- }
-
- private void FixedUpdate()//刚体运动放在FixUpdate里
- {
- //定义每秒几米的速度
- rig.velocity = new Vector2(5 * dir, GetComponent<Rigidbody2D>().velocity.y);
-
- //方法一:通过Tag
- Collider2D hit = Physics2D.OverlapPoint(front.position);
- if (hit != null)
- {
- if (hit.tag == "Wall")
- {
- Flip();
- }
- }
-
- //方法二:通过定义碰撞物体的层来实现转身
- //在第六层里碰撞物体tag=Wall时转身
- //Collider2D[] hits = Physics2D.OverlapPointAll(front.position, 1<<6);
- //foreach(var item in hits)
- //{
- // if (item.tag == "Wall")
- // {
- // Flip();
- // break;
- // }
- //}
- }
-
- void Flip()
- {
- //利用localScale镜像
- Vector2 v = transform.localScale;
- v.x *= -1;
- transform.localScale = v;
- dir *= -1;
- }
-
- void Update()
- {
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。