链接:https://www.bilibili.com/video/BV1i4411m7fK?spm_id_from=pageDriver
创建一个父类Enemy脚本↓↓↓↓↓,敌人需要功能从该脚本调用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy2 : MonoBehaviour
{
protected Rigidbody2D rb;//定义变量以protected替换public
protected Collider2D coll;
protected Animator anim;
protected virtual void Start()//void Start()前添加protected virtual,子类则添加protected override;
{
coll = GetComponent<Collider2D>();
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
public void Death()
{
Destroy(gameObject);
}
public void JumpOn()
{
anim.SetTrigger("death");
coll.enabled = false;
rb.velocity = new Vector2(0, 0);
}
}
下面是Frog脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_Frog2 : Enemy2
{
//private Rigidbody2D rb;
//private Collider2D coll;
//private Animator anim;//这三行因为父类已经调用了,下面base.Start();即可调用。
private float pointLeft, pointRight;
private bool faceLeft = true;
public Transform pointLeftREF, pointRightREF;
public float speed, jumpForce;
public LayerMask Ground;
protected override void Start()
{
base.Start();
//rb = GetComponent<Rigidbody2D>();
//coll = GetComponent<Collider2D>();
//anim = GetComponent<Animator>();
pointLeft = pointLeftREF.transform.position.x;
pointRight = pointRightREF.transform.position.x;
Destroy(pointLeftREF.gameObject);
Destroy(pointRightREF.gameObject);
}
void Update()
{
SwitchAnim();
}
void Movement()
{
if (faceLeft)
{
if (coll.IsTouchingLayers(Ground))
{
anim.SetBool("jumping", true);
rb.velocity = new Vector2(-speed, jumpForce);
}
}
else
{
if (coll.IsTouchingLayers(Ground))
{
anim.SetBool("jumping", true);
rb.velocity = new Vector2(speed, jumpForce);
}
}
}
void SwitchAnim()
{
if (coll.enabled)
{
if (anim.GetBool("jumping"))
{
if (rb.velocity.y < 0.1f)
{
anim.SetBool("falling", true);
anim.SetBool("jumping", false);
}
}
if (anim.GetBool("falling") && coll.IsTouchingLayers(Ground))
{
anim.SetBool("falling", false);
if (transform.position.x < pointLeft)
{
faceLeft = false;
transform.localScale = new Vector3(-1, 1, 1);
}
if (transform.position.x > pointRight)
{
faceLeft = true;
transform.localScale = new Vector3(1, 1, 1);
}
}
}
else
{
rb.velocity = new Vector2(0, 0);
}
}
}
下面是Eagle脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_Eagle2 : Enemy2
{
public Transform pointTop, pointBottom;//获取eagle顶部和底部参考点坐标
public float speed;
private float top, bottom;
protected override void Start()
{
base.Start();//因为在夫类已经制定了组件,改句功能就是调用start内语句
top = pointTop.position.y;
bottom = pointBottom.position.y;//先赋值
Destroy(pointTop.gameObject);
Destroy(pointBottom.gameObject);//然后销毁参考坐标的物体
}
void Update()
{
if (coll.enabled)
{
rb.velocity = new Vector2(0, speed);
if (transform.position.y > top)
{
transform.position = new Vector2(transform.position.x, top - 0.5f);
speed = -speed;
}
if (transform.position.y < bottom)
{
transform.position = new Vector2(transform.position.x, bottom + 0.5f);
speed = -speed;
}
}
}
}
Player在JumpOn调用Enemy函数:
private void OnCollisionEnter2D(Collision2D collision)//踩踏事件判定下落时和标签为Enemy
{
if (collision.gameObject.tag == "Enemy")
{
//Enemy_Frog frog = collision.gameObject.GetComponent<Enemy_Frog>(); //Course17前部分;生成一个叫frog的实体,当调用frog时可以调用Enemy_Frog里的所有代码
Enemy enemy = collision.gameObject.GetComponent<Enemy>();//Course17-5,调用一个父类Enemy生成一个实体enemy
if (anim.GetBool("falling"))
{
//这里敌人销毁不用,因为已经在Enemy_Frog里调用了销毁的语句JumpOn 。Destroy(collision.gameObject);
//frog.JumpOn();
enemy.JumpOn();//Course17-5,调用时使用实体enemy
rb.velocity = new Vector2(rb.velocity.x, jumpforce * Time.fixedDeltaTime);
anim.SetBool("jumping", true);
}
else
if (transform.position.x < collision.gameObject.transform.position.x)
{
rb.velocity = new Vector2(-5, rb.velocity.y);
anim.SetBool("hurt", true);
isHurt = true;
}
else
if (transform.position.x > collision.gameObject.transform.position.x)
{
rb.velocity = new Vector2(5, 4);
anim.SetBool("hurt", true);
isHurt = true;
}
}
}
调用一个父类Enemy生成一个实体enemy↓
Enemy enemy = collision.gameObject.GetComponent<Enemy>();
enemy.JumpOn();
直接调出父类里面的JumpOn();
记:
velocity需要在前面添加rb,transform不需要.eg:
rb.velocity = new Vector2(0, 0);
transform.localScale = new Vector3(-1, 1, 1);