美文网首页
Unity项目-水果忍者

Unity项目-水果忍者

作者: 元宇宙协会 | 来源:发表于2018-07-12 21:38 被阅读241次
    • 创建刀光
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class CutFruit : MonoBehaviour {
    
        private GameObject knife;
        private Vector3 startPos;
        private Vector3 endPos;
        private GameObject canver;
        void Start () {
            knife =  (GameObject)Resources.Load("Prefabs/Knife");
            canver = GameObject.Find("Canvas");
            Debug.Log(knife.name);
        }
        
        
        void Update () {
            if (Input.GetMouseButtonDown(0))
            {
                startPos = Input.mousePosition;
            }
            if (Input.GetMouseButtonUp(0))
            {
                endPos = Input.mousePosition;
    
                if (endPos != startPos)
                {
                    // 计算夹角
                    float angle = (endPos.y - startPos.y) / (endPos.x - startPos.x);
                    float angle1 = Mathf.Atan(angle);
                    float angle2 = angle1 * 180 / Mathf.PI;
    
                    // 计算位置
                    Vector3 knifPos =(startPos + (endPos - startPos) / 2);
    
                    // 实例化
                    GameObject knif = Instantiate(knife);
                    knif.transform.position = knifPos;
                    knif.transform.rotation = Quaternion.AngleAxis(angle2, Vector3.forward);
                    knif.transform.parent = canver.transform;
                    Destroy(knif, 1.5f);
                }    
            }
        }
    }
    
    • 创建水果生成器
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Friut : MonoBehaviour {
    
        public GameObject canvas;
        // 水果集合
        public  GameObject[] fruits;
    
        void Start () {
            canvas = GameObject.Find("Canvas");
            // 定时执行某个方法,(方法名,多少秒后执行,重复执行间隔)
            InvokeRepeating("Creat", 0f, 4f);
        }
    
        private void Creat()
        {
            int index = Random.Range(0, 4);
            GameObject fruit = Instantiate(fruits[index]) as GameObject;
            fruit.transform.parent = canvas.transform;
            fruit.GetComponent<RectTransform>().position = new Vector3(Random.Range(35f, Screen.width - 35f), 50, 0);
            fruit.AddComponent<Rigidbody>().AddForce(new Vector3(0f, 90f, 0f), ForceMode.Impulse);
        }
    }
    
    
    using UnityEngine;
    // 刀光对象检测物体
    public class CutTrigger : MonoBehaviour {
    
        
        void Start () {
            
        }
    
        private void OnTriggerEnter(Collider other)
        {
            // 可以获取水果类型了
            Debug.Log(other.name);
            if (other.tag == "Pear")
            {   
                InitFruit("Prefabs/pear01", other.gameObject); 
                // 分数加1
            }
            if (other.tag == "Apple")
            {
                InitFruit("Prefabs/apple01", other.gameObject);
                // 分数加1
            }
            if (other.tag == "Banana")
            {
                InitFruitTwo("Prefabs/banana", other.gameObject);
                // 分数加2
            }
            if (other.tag == "Gapple")
            {
                InitFruit("Prefabs/gapple01", other.gameObject);
                // 分数加10
            }
            if (other.tag == "Life")
            {
                InitFruitTwo("Prefabs/life", other.gameObject);
                // 生命-1
            }
    
        }
        // 一种图片水果
        public void InitFruit(string path, GameObject obj)
        {
            // 切到水果了,创建切后的水果对象
            GameObject fruit = Instantiate(Resources.Load(path) as GameObject);
            fruit.transform.position = obj.transform.position;
            fruit.transform.parent = obj.transform.parent;
            fruit.transform.rotation = obj.transform.rotation;
            // 销毁原水果
            Destroy(obj);
            // 销毁切后的水果对象
            Destroy(fruit, 0.2f);
        }
        // 两种图片水果
        public void InitFruitTwo(string path, GameObject obj)
        {
            // 切到水果了,创建切后的水果对象
            GameObject fruit = Instantiate(Resources.Load(path + "02") as GameObject);
            fruit.transform.position = obj.transform.position - new Vector3(0,35,0);
            fruit.transform.parent = obj.transform.parent;
            fruit.transform.rotation = obj.transform.rotation;
            GameObject fruit1 = Instantiate(Resources.Load(path + "01" ) as GameObject);
            fruit1.transform.position = obj.transform.position - new Vector3(0, 65, 0);
            fruit1.transform.parent = obj.transform.parent;
            fruit1.transform.rotation = obj.transform.rotation;
            // 销毁原水果
            Destroy(obj);
            // 销毁切后的水果对象
            Destroy(fruit, 0.2f);
            Destroy(fruit1, 0.2f);
        }
    }
    

    相关文章

      网友评论

          本文标题:Unity项目-水果忍者

          本文链接:https://www.haomeiwen.com/subject/cvqdpftx.html