unity自学的第四天

一、敌人类的脚本

1、HP组件(生命)
2、fight组件(攻击)
3、enemymove组件(移动)

二、具体代码

1.HP

public class HP : MonoBehaviour
{
    public int hppoint = 100;
    void Update()
    {
        if (hppoint <= 0) Destroy(gameObject);//gameObject为物体
    }
    public void HpCutdown(int x)//掉血
    {
        hppoint = hppoint - x;
    }
}

2.Fight

public class fight : MonoBehaviour
{
    private void OnCollisionEnter(Collision collision) {
        if (collision.gameObject.tag == "Player")//如果碰撞到标签为“Player”就执行下面的
        {
            collision.gameObject.GetComponent<HP>().HpCutdown(100);
        }

    }

}

3.Enemymove

public class enemymove : MonoBehaviour
{
    GameObject go;
    public float speed;
    public float look = 100;

    void Update()
    {
        go = GameObject.FindGameObjectWithTag("Player");
        Vector3 vec = go.transform.position - this.transform.position;//要去的位置减去现在的位置
        float length = vec.sqrMagnitude;//路径长度
        vec.y = 0;
        if (length < look)
        {
            transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(vec), 0.3f);//转向标签为“Player”的游戏对象
            transform.position = transform.position + this.transform.forward * speed * Time.deltaTime;
        }

    }
}

总结

要不断加强对三维数学的学习,对于敌人移动的计算还要继续深入。