Unity3D作业六项目二——射飞碟

这次作业仅仅实现了基本的功能,基本上理解了UFO这次作业就很简单了

坑主要在于碰撞器的设置以及一些预设的问题。

主要思路就是在发射箭的同时,把CollisionRev : MonoBehaviour 附加上去,当撞击的时候,由他传递参数给CCompent : MonoBehaviour,来改变箭的状态。

这次的封装应该是可以的,嘻嘻。



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyGame;
public class CollisionRev : MonoBehaviour {

    private void OnCollisionEnter(Collision collision)
    {
        this.gameObject.GetComponent<CCompent>().SSend(true);
        Debug.Log("OK");
    }
}

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

public class CCompent : MonoBehaviour {

	private bool  isCrash = false;
    private float time;
    ObejctFactory OF;
    SceneControl SC;
    private void Awake()
    {
        OF = Singleton<ObejctFactory>.Instance;
        SC = Singleton<SceneControl>.Instance;
        time = Time.time;
    }
    public void SSend(bool  k)
    {
        isCrash = k;
    }

    void FixedUpdate()
    {
        Rigidbody rigid = this.gameObject.GetComponent<Rigidbody>();
        if (isCrash != false)
        {
           
                SC.Hit();
            Destroy(GetComponent<Rigidbody>()); //使得箭停在靶子上  
            Destroy(this);
        }
      else if(Time.time - time > 10)
        {
            this.gameObject.GetComponent<Transform>().position = new Vector3(1000f, 1000f, 1000f);
            Destroy(this);
        }
    }

}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using MyGame;
public class UI : MonoBehaviour {

    public Text Score_Text;  // 显示得分  
    public Text Wind_Text;  // 显示风力
    public Camera cam;
    ObejctFactory OF;
    SceneControl SC;
    ActionControl AC;

    private void Awake()
    {
        OF = Singleton<ObejctFactory>.Instance;
        SC = Singleton<SceneControl>.Instance;
        AC = Singleton<ActionControl>.Instance;
    }
    void Start()
    {
        Score_Text.text = "Score:  ";
       
    }

    void FixedUpdate()
    {
       
        if (Input.GetMouseButtonDown(0))
        {
            Vector3 mp = Input.mousePosition;
            Ray ray = Camera.main.ScreenPointToRay(mp);    // 摄像机到鼠标射线  
            GameObject temp = OF.GetArrow();
            if (temp.GetComponent<Rigidbody>() == null)
                temp.AddComponent<Rigidbody>();
           temp.GetComponent<Rigidbody>().velocity = ray.direction * 50;
            temp.GetComponent<Transform>().position =new Vector3(0,0,-10);
            Debug.Log(ray.direction);
            AC.wind(temp, ray.direction);
            CCompent ac = temp.AddComponent<CCompent>();
            temp.AddComponent<CollisionRev>();

        }
        Score_Text.text = "Score: " + SC.getScore();
        Wind_Text.text = "Wind: " + SC.getWind();
    }



}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyGame;
public class ObejctFactory : MonoBehaviour {

    public GameObject arrow;
    private List<GameObject> Arrow_Active_List = new List<GameObject>();
    private void Start()
    {
        for (int i = 0; i < 10; i++)
        {
            GameObject Temp = GameObject.Instantiate(arrow, new Vector3(1000f,1000f,1000f), Quaternion.identity) as GameObject;
            //Temp.GetComponent<Transform>().Rotate(new Vector3(0, 90, 0));
            Arrow_Active_List.Add(Temp);
        }
    }

    public GameObject GetArrow()
    {
            GameObject temp = Arrow_Active_List[0];
            Arrow_Active_List.Remove(temp);
            Arrow_Active_List.Add(temp);
            return temp;
    }

  





}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyGame;
public class ActionControl : MonoBehaviour {

    private float Force = 0f;
    SceneControl SC;
    private void Awake()
    {
        SC = Singleton<SceneControl>.Instance;
    }
    public void wind(GameObject arrow,Vector3 dir)
    {
        Force = Random.Range(0,7);
        arrow.GetComponent<Rigidbody>().AddForce(dir *30, ForceMode.Impulse);
        arrow.GetComponent<Rigidbody>().AddForce(Vector3.left * Force, ForceMode.Impulse);
        SC.setWind(Force);
    }

    public float getWindForce()
    {
        return Force;
    }

}

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


namespace MyGame
{

    //场景单实例
    public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
    {

        protected static T instance;

        public static T Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = (T)FindObjectOfType(typeof(T));
                    if (instance == null)
                    {
                        Debug.LogError("An instance of " + typeof(T)
                            + " is needed in the scene, but there is none.");
                    }
                }
                return instance;
            }
        }
    }

    public class SceneControl : MonoBehaviour
    {
        private int Now_Score = 0;   //当前分数
        private float Force = 0f;   //风力
        ActionControl AC;
        private void Awake()
        {
            AC = Singleton<ActionControl>.Instance;
        }


        public void Hit()
        {
            Now_Score ++;
        }

        public int getScore()
        {
            return Now_Score;
        }
        public float getWind()
        {
            return Force;
        }
        public void  setWind(float x)
        {
            Force = x;
            Debug.Log(Force);
        }


    }
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值