【Unity】My First Game

    科技2022-07-13  128

    敌人移动

    在”Frog“的”Inspector窗口“中添加”New script“,命名为”Enemy_Frog.cs“。

    在”Frog“中”Create Empty“,分别命名为”left“和”right“,调整位置(如果看不见点,在”Scene窗口“中点击”Gizmos“即可显示)。

    打开”Enemy_Frog.cs“,添加代码:

    private Rigidbody2D rb; public Transform leftPoint, rightPoint;

    将”Frog“的”left“和”right“选进“Script窗口”的“leftPoint”和“rightPoint”中。、

    添加代码:

    public float speed; private float leftx, rightx;//记录leftPoint, rightPoint的位置 private bool faceLeft = true;//默认瓜娃子面向左

    添加Start方法:

    void Start() { rb = GetComponent<Rigidbody2D>();//获得刚体 transform.DetachChildren(); //注意这句,当敌人转向时,其孩子leftPoint, rightPoint也随之转向,导致Frog抽搐 //DetachChildren所有子物体解除父子关系 //当游戏开始后,leftPoint, rightPoint不在是Frog的孩子,就不会随之反转 leftx = leftPoint.position.x;//记录leftPoint, rightPoint的位置 rightx = rightPoint.position.x; Destroy(leftPoint.gameObject);//记录完位置后就销毁两点 Destroy(rightPoint.gameObject); }

    添加Movement方法:

    void Update() { Movement(); } void Movement() { if (faceLeft) { rb.velocity = new Vector2(-speed,rb.velocity.y);//向左移动为-speed if (transform.position.x < leftx)//Frog到达leftPoint { transform.localScale=new Vector3(-1,1,1);//令x==-1使其朝向右 faceLeft = false; } } else { rb.velocity = new Vector2(speed, rb.velocity.y); if (transform.position.x > rightx) { transform.localScale=new Vector3(1,1,1); faceLeft = true; } } }

    保存,瓜娃子就动起来了,将“Frog”移动到“Prefabs文件夹”中,就可重复使用。

    敌人动画

    点击Prefabs文件夹中的“Frog”,在Animation窗口中分别添加”idle“、”jump“、”fall“三个动画,并设置好Samples

    “Animator窗口“中分别连接“idle -> jump”、“jump -> fall”、“fall -> idle”,并设置好条件与相关参数

    在idle动画中结束点”Add event“,并在inspector窗口中在”function“中添加”Enemy_Frog.cs“中的Movement()方法,此举实现当idle动画结束后才进行jump动画

    打开Enemy_Frog.cs,添加代码:

    private Animator anim; private Collider2D coll; public LayerMask ground; public float speed, jumpForce;

    在Frog的inspector窗口中的Script选项的Ground选择已经创建好的”Ground“

    在Start方法中添加代码:

    anim = GetComponent<Animator>(); coll = GetComponent<Collider2D>();

    删除Update方法中调用Movement方法语句,并调用SwitchAnim方法

    修改Movement方法

    void Movement() { if (faceLeft) { if (coll.IsTouchingLayers(ground))//判断是否触碰到地面 { if (transform.position.x < leftx) { transform.localScale=new Vector3(-1,1,1); faceLeft = false; } anim.SetBool("jumping",true); rb.velocity = new Vector2(-speed,jumpForce); } } else { if (coll.IsTouchingLayers(ground)) { if (transform.position.x > rightx) { transform.localScale=new Vector3(1,1,1); faceLeft = true; } anim.SetBool("jumping",true); rb.velocity = new Vector2(speed, jumpForce); } } }

    添加SwitchAnim方法:

    void SwitchAnim() { if (anim.GetBool("jumping")) { if (rb.velocity.y < 0.1) { anim.SetBool("jumping",false); anim.SetBool("falling",true); } } if (coll.IsTouchingLayers(ground) && anim.GetBool("falling")) { anim.SetBool("falling",false); } }

    以下是死亡动画的步骤:先正常在Frog的Animation中添加“die”动画,并“Any State -> die”,在Parameters中添加一个“Trigger”为“death”,并将条件设置为“death”,调整相关参数

    在”Enemy_Frog.cs“中添加代码:

    void Death() { Destroy(gameObject);//在die动画后添加event调用Death() } public void JumpOn() { //在PlayerController中调用该方法 anim.SetTrigger("death");//开启die的条件 }

    修改”Player Controler.cs“的“OnCollisionEnter2D(Collision2D collision)”方法中的代码:

    if (collision.gameObject.tag == "Enemy") { Enemy_Frog frog = collision.gameObject.GetComponent<Enemy_Frog>(); //创建Enemy_Frog类的实例 if(anim.GetBool("falling")) { frog.JumpOn();调用frog的死亡动画 rb.velocity = new Vector2(rb.velocity.x, jumpForce * Time.deltaTime); anim.SetBool("jumping", true); } else if (transform.position.x < collision.gameObject.transform.position.x) { rb.velocity = new Vector2(-10,rb.velocity.y); isHurt = true; } else if (transform.position.x > collision.gameObject.transform.position.x) { rb.velocity=new Vector2(10,rb.velocity.y); isHurt = true; } }

    以下是有关父子级的知识点,创建一个新类“Enemy”,修改”Enemy_Frog.cs“中代码:

    public class Enemy_Frog : Enemy//继承自Enemy类,而不是MonoBehavior { …… }

    在“Enemy”中添加代码:

    protected Animator anim; // Start is called before the first frame update protected virtual void Start()//注意使用protected virtual,使子级能够重写该方法 { anim = GetComponent<Animator>(); } public void Death()//注意使用public { Destroy(gameObject);//在die动画后添加event调用Death() } public void JumpOn()//注意使用public { anim.SetTrigger("death"); }

    在“Enemy_Frog”中修改代码:

    protected override void Start()//注意使用protected override { base.Start();//获得父级的Start() rb = GetComponent<Rigidbody2D>(); coll = GetComponent<Collider2D>(); transform.DetachChildren(); leftx = leftPoint.position.x; rightx = rightPoint.position.x; Destroy(leftPoint.gameObject); Destroy(rightPoint.gameObject); }

    修改”Player Controler.cs“的“OnCollisionEnter2D(Collision2D collision)”方法中的代码:

    /*Enemy_Frog frog = collision.gameObject.GetComponent<Enemy_Frog>();//调用frog的死亡动画*/ Enemy enemy = collision.gameObject.GetComponent<Enemy>(); …… /*frog.JumpOn();*/ enemy.JumpOn();
    Processed: 0.017, SQL: 8