您的位置:首页 > 移动开发 > Unity3D

Unity Action Game Demo(1)

2015-08-15 21:43 696 查看

Unity Action Game Demo(1)

1、实现摄像机跟随主角。

FollowPlayer.cs

using UnityEngine;
using System.Collections;

public class FollowPlayer : MonoBehaviour
{

    private Transform player;
    public float speed = 100;

    // Use this for initialization
    void Start()
    {
        player = GameObject.FindGameObjectWithTag(Tags.player).transform;
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 targetPos = player.position + new Vector3(1.93f, 1.87f, 0.76f);
        transform.position = Vector3.Lerp(transform.position, targetPos, speed * Time.deltaTime);

    }
}


2、控制主角的运动。

PlayerMove.cs

using UnityEngine;
using System.Collections;

public class PlayerMove : MonoBehaviour {
    private CharacterController cc;
    public float speed = 10;

    private Animator animator;
    void Awake()
    {
        cc = this.GetComponent<CharacterController>();
        animator = this.GetComponent<Animator>();
    }

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
    void Update()
    {

        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        if (Joystick.h != 0 || Joystick.v != 0)
        {
            h = Joystick.h;
            v = Joystick.v;
        }
        if (Mathf.Abs(h) > 0.1f || Mathf.Abs(v) > 0.1f)
        {
            animator.SetBool("walk", true);
            if (animator.GetCurrentAnimatorStateInfo(0).IsName("PlayerRun"))
            {
                Vector3 targetDir = new Vector3(-v, 0, h);
                transform.LookAt(transform.position + targetDir);
                cc.SimpleMove(targetDir * speed);
            }

        }
        else
        {
            animator.SetBool("walk", false);
        }
    }
}


3、主角动画控制。

PlayerAnimation.cs

using UnityEngine;
using System.Collections;

public class PlayerAnimation : MonoBehaviour {

    private Animator animator;
    private bool isCanAttackB;
	// Use this for initialization
	void Start () {

        animator = this.GetComponent<Animator>();
        EventDelegate NormalAttackEvent = new EventDelegate(this,"OnNormalAttackClick");
        GameObject.Find("normal").GetComponent<UIButton>().onClick.Add(NormalAttackEvent);

        EventDelegate RangeAttackEvent = new EventDelegate(this, "OnRangeAttackClick");
        GameObject.Find("rangeAttack").GetComponent<UIButton>().onClick.Add(RangeAttackEvent);

        EventDelegate RedAttackEvent = new EventDelegate(this, "OnRedAttackClick");
        GameObject.Find("redAttack").GetComponent<UIButton>().onClick.Add(RedAttackEvent);

        GameObject.Find("redAttack").gameObject.SetActive(false);
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    public void OnNormalAttackClick()
    {
        if (animator.GetCurrentAnimatorStateInfo(0).IsName("PlayerAttackA")
            && isCanAttackB)
        {
            animator.SetTrigger("AttackB");
            Debug.Log("------>>>BBBBBB");
        }
        else
        {
            
            animator.SetTrigger("AttackA");
        }
    }
    public void OnRangeAttackClick()
    {
        Debug.Log("OnRangeAttackClick");
        animator.SetTrigger("AttackRange");
    }
    public void OnRedAttackClick()
    {
        Debug.Log("OnRedAttackClick");
        animator.SetTrigger("AttackA");
    }

    public void AttackBEvent1()
    {
        isCanAttackB = true;
    }
    public void AttackBEvent2()
    {
        isCanAttackB = false;
    }
}


4、怪物的产生。

Spawn.cs

using UnityEngine;
using System.Collections;

public class Spawn : MonoBehaviour {

    public GameObject prefab;

    public GameObject creatSpawn()
    {
        return GameObject.Instantiate(prefab, transform.position, transform.rotation) as GameObject;
    }
}


SpawnManager.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SpawnManager : MonoBehaviour {

    public static SpawnManager _instance;
    public Spawn[] monsterSpawnArray;
    public Spawn[] bossSpawnArray;

    public List<GameObject> enemyList = new List<GameObject>();

    void Awake()
    {
        _instance = this;
    }
    void Start()
    {
        StartCoroutine(EnemySpawn());
    }
    IEnumerator EnemySpawn()
    {
        foreach (Spawn s in monsterSpawnArray)
        {
            enemyList.Add(s.creatSpawn());
        }

        while (enemyList.Count > 0)
        {
            yield return new WaitForSeconds(2f);
        }

        foreach (Spawn s in monsterSpawnArray)
        {
            enemyList.Add(s.creatSpawn());
        }

        yield return new WaitForSeconds(2f);

        foreach (Spawn s in monsterSpawnArray)
        {
            enemyList.Add(s.creatSpawn());
        }

        while (enemyList.Count > 0)
        {
            yield return new WaitForSeconds(2f);
        }

        foreach (Spawn s in monsterSpawnArray)
        {
            enemyList.Add(s.creatSpawn());
        }

        yield return new WaitForSeconds(2f);

        foreach (Spawn s in monsterSpawnArray)
        {
            enemyList.Add(s.creatSpawn());
        }

        yield return new WaitForSeconds(2f);

        foreach (Spawn s in bossSpawnArray)
        {
            enemyList.Add(s.creatSpawn());
        }

    }
}


5、实现怪物的跟随。

SoulMoster.cs

using UnityEngine;
using System.Collections;

public class SoulMoster : MonoBehaviour {

    private Transform player;

    private PlayerATKAndDamage playerATKAndDamage;
    public float attackDistance = 1f;
    public float speed = 30;
    private CharacterController cc;

    private Animator animator;

    public float attackTime = 3f;
    private float attackTimer = 0;
    void Start()
    {
        cc = this.GetComponent<CharacterController>();
        player = GameObject.FindGameObjectWithTag(Tags.player).transform;
        playerATKAndDamage = player.GetComponent<PlayerATKAndDamage>();
        animator = this.GetComponent<Animator>();
        attackTimer = attackTime;
        
    }
	// Use this for initialization

	
	// Update is called once per frame
	void Update () {
        if (playerATKAndDamage.hp <= 0)
        {
            animator.SetBool("Walk", false);
            return;
        }

        Vector3 targetPos = player.position;
        targetPos.y = transform.position.y;
        transform.LookAt(targetPos);

        float distance = Vector3.Distance(targetPos, transform.position);
        if (distance <= attackDistance)
        {
            attackTimer += Time.deltaTime;
            if (attackTimer > attackTime)
            {
                animator.SetBool("Attack", true);
                attackTimer = 0;
            }
            else
            {
                animator.SetBool("Walk", false);
            }
        }
        else
        {
            attackTimer = attackTime;
            if (animator.GetCurrentAnimatorStateInfo(0).IsName("MonRun"))
            {

                cc.SimpleMove(transform.forward * speed * Time.deltaTime);
            }
            animator.SetBool("Walk", true);
        }

	}
}


6、伤害值的计算。

ATKAndDamage.cs

using UnityEngine;
using System.Collections;

public class ATKAndDamage : MonoBehaviour {

    public float hp = 100;
    public float normalAttack = 50;

    public float attackDistance = 1;

    public Animator animator;

    public void Awake()
    {
        animator = this.GetComponent<Animator>();
    }
    public virtual void TakeDamage(float damage)
    {
        if (hp > 0)
        {
            hp -= damage;
        }
        if (hp > 0)
        {
            animator.SetTrigger("Damage");
        }
        else
        {
            animator.SetBool("Dead", true);
            if (this.tag == Tags.SoulBoss || this.tag == Tags.SoulMonster)
            {
                SpawnManager._instance.enemyList.Remove(this.gameObject);
                this.GetComponent<CharacterController>().enabled = false;
                Destroy(this.gameObject, 1);
            }

        }
        GameObject.Instantiate(Resources.Load("HitBoss"), transform.position + Vector3.up, transform.rotation);
    }
}


PlayerATKAndDamage.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlayerATKAndDamage : ATKAndDamage {

    public float attackB = 80;
    public float attackRange = 100;
    public void AttackA()
    {
        
        GameObject enemy = null;
        float distance = attackDistance;
        foreach (GameObject go in SpawnManager._instance.enemyList)
        {
            float temp = Vector3.Distance(go.transform.position, transform.position);
            if (temp < distance)
            {
                enemy = go;
                distance = temp;
            }
        }
        if (enemy == null)
        {

        }
        else
        {
            Vector3 targetPos = enemy.transform.position;
            targetPos.y = transform.position.y;
            transform.LookAt(targetPos);
            enemy.GetComponent<ATKAndDamage>().TakeDamage(normalAttack);
        }
    }
    public void AttackB()
    {

        GameObject enemy = null;
        float distance = attackDistance;
        foreach (GameObject go in SpawnManager._instance.enemyList)
        {
            float temp = Vector3.Distance(go.transform.position, transform.position);
            if (temp < distance)
            {
                enemy = go;
                distance = temp;
            }
        }
        if (enemy == null)
        {

        }
        else
        {
            Vector3 targetPos = enemy.transform.position;
            targetPos.y = transform.position.y;
            transform.LookAt(targetPos);
            enemy.GetComponent<ATKAndDamage>().TakeDamage(attackB);
        }
    }
    public void AttackRange()
    {
       
        float distance = attackDistance;
        List<GameObject> husheng = new List<GameObject>();
        foreach (GameObject go in SpawnManager._instance.enemyList)
        {
            float temp = Vector3.Distance(go.transform.position, transform.position);
            if (temp < distance)
            {
                husheng.Add(go);
                
            }
        }
        foreach (GameObject go in husheng)
        {
            go.GetComponent<ATKAndDamage>().TakeDamage(attackRange);
        }
        
    }
    public void AttackGUN()
    {

    }
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}


SoulBossATKAndDamage.cs

using UnityEngine;
using System.Collections;

public class SoulBossATKAndDamage : ATKAndDamage {

    private Transform player;

    void Awake()
    {
        base.Awake();
        player = GameObject.FindGameObjectWithTag(Tags.player).transform;
    }
    public void Attack1()
    {
        if (Vector3.Distance(transform.position, player.position) < attackDistance)
        {
            player.GetComponent<ATKAndDamage>().TakeDamage(normalAttack);
        }

    }
    public void Attack2()
    {
        if (Vector3.Distance(transform.position, player.position) < attackDistance)
        {
            player.GetComponent<ATKAndDamage>().TakeDamage(normalAttack);
        }
    }
}


SoulMonsterATKAndDamage.cs

using UnityEngine;
using System.Collections;

public class SoulMonsterATKAndDamage : ATKAndDamage {
    private Transform player;

    void Awake()
    {
        base.Awake();
        player = GameObject.FindGameObjectWithTag(Tags.player).transform;
    }

    public void MonAttack()
    {
        if (Vector3.Distance(transform.position, player.position) < attackDistance)
        {
            player.GetComponent<ATKAndDamage>().TakeDamage(normalAttack);
        }
    }

    public void Update()
    {
        if (hp <= 0)
        {
            
        }
    }
}


7、标签管理。

Tags.cs

using UnityEngine;
using System.Collections;

public class Tags{

    public const string player = "cc";
    public const string SoulBoss = "SoulBoss";
    public const string SoulMonster = "mm";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: