2017年12月30日 星期六

吳映廷 計算機圖學 week16

本週的主題是製作期末作品。
這次開始使用Unity,把主角和基本的game manager 弄好了。
以下是基本的game manager程式碼。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GM : MonoBehaviour
{
    public static GM instance;
 
    void Awake()
    {

        {
            if (instance == null)

                instance = this;

            else if (instance != this)
                Destroy(gameObject);
        }
        Application.targetFrameRate = 60;
    }
void Update()
    {
        //每frame會自動呼叫一次的函式
    }
}
以下是主角的程式碼。
public class stick_man : MonoBehaviour
{
    public bool isAttack = false;
    public bool isAttack_R = false;
    public float attackTime = 0,attackTime_R = 0;
    private bool isDead = false;         

    private Animator anim;               
    private Rigidbody2D rb2d;
    void Start()
    {
        anim = GetComponent<Animator>();
        rb2d = GetComponent<Rigidbody2D>();
    }
    void Update()
    {
        if (isDead == false)
        {
            if (Input.GetKeyDown(KeyCode.LeftArrow))
            {
                Vector3 theScale = transform.localScale;
                theScale.x = Math.Abs(theScale.x);
                transform.localScale = theScale;

                System.Random rnd = new System.Random();
                var attackType = rnd.Next(0, 3);

                isAttack = true;

                switch (attackType)
                {
                    case 0:
                        anim.SetTrigger("punch");
                        break;
                    case 1:
                        anim.SetTrigger("elbow");
                        break;
                    case 2:
                        anim.SetTrigger("kick");
                        break;

                }

            }
            if (Input.GetKeyDown(KeyCode.RightArrow))
            {
                Vector3 theScale = transform.localScale;
                theScale.x = -Math.Abs(theScale.x);
                transform.localScale = theScale;

                System.Random rnd = new System.Random();
                var attackType = rnd.Next(0, 3);

                isAttack_R = true;

                switch (attackType)
                {
                    case 0:
                        anim.SetTrigger("punch");
                        break;
                    case 1:
                        anim.SetTrigger("elbow");
                        break;
                    case 2:
                        anim.SetTrigger("kick");
                        break;

                }

            }
            if (attackTime >= 1.5)
            {
                isAttack = false;
                attackTime = 0;
            }
            if (attackTime_R >= 1.5)
            {
                isAttack_R = false;
                attackTime_R = 0;
            }
            attackTime += Time.deltaTime;
            attackTime_R += Time.deltaTime;

        }
        if (isDead == true)
        {
            Destroy(this.gameObject);
        }
    }
}

沒有留言:

張貼留言