Unity3d跳一跳小游戏简单代码

分类栏目:unity3d教程

156

一款 Unity3d 制作的跳一跳 先放下项目代码
https://pan.baidu.com/s/1r7ARsrkRTUkj7GxuXatckg 提取码: 6w4a 

1.跳一跳小人代码

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Move : MonoBehaviour {
    GameC g;
    public float time1;
    public float time2;
    public float time3;
    public float time4;
    // Use this for initialization
    void Start() {
        g = GameObject.Find("GameController").GetComponent<GameC>();
    }
 
    // Update is called once per frame
    void Update() {
        //jump();
        if (g.flag == false) {
            jump();
 
        } else {
            jump1();
        }
    }
 
 
    private void OnCollisionEnter(Collision other) {
        if (other.transform.tag == "Cube") {
            g.ShengC();
 
        }
        if (other.transform.tag == "Plane") {
            SceneManager.LoadScene(0);
 
        }
    }
    private void OnCollisionExit(Collision collision) {
        if (collision.transform.tag == "Cube") {
            collision.transform.tag = "Finish";
        }
    }
 
    public void jump() {          //如果手指大于0  && 第一个手指     持续按压
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary) {
            /*if (Input.GetButton("Jump"))*/
            Rigidbody r = GetComponent<Rigidbody>();
            r.AddForce(Vector3.right * Time.deltaTime * time1, ForceMode.Impulse); //getbutton运行长,timec长
            r.AddForce(Vector3.up * Time.deltaTime * time2, ForceMode.Impulse);
        }
    }
 
 
    public void jump1() {
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
       /* if (Input.GetButton("Jump"))*/ {
            Rigidbody r = GetComponent<Rigidbody>();
            r.AddForce(Vector3.forward * Time.deltaTime * time3, ForceMode.Impulse); //getbutton运行长,timec长
            r.AddForce(Vector3.up * Time.deltaTime * time4, ForceMode.Impulse);
 
        }
    }
 
}
 
2.GameController

using UnityEngine;
using System.Collections;
 
public class GameC : MonoBehaviour {
    public GameObject player;
    public GameObject box;
    GameObject cubeClone;
    Vector3 vec = Vector3.zero;
 
    //Move jump;
 
    public bool flag = false;
    //public GameObject[] Sjs = { 1, 2, 3, 4, 5, 6, 7, 8 };
    // Use this for initialization
    void Start() {
        //jump = GameObject.Find("Capsule").GetComponent<Move>();
    }
 
    // Update is called once per frame
    void Update() {
        //Camera.main.transform.LookAt(box.transform .position);
    }
 
    //public void Slh() {
 
 
    //   cubeClone= (GameObject)Instantiate(box, new Vector3(box.transform.position.x + Random.Range(2, 5), 0, 0)+vec
    //       , box.transform.rotation);
    //    vec = cubeClone.transform .position  - box.transform.position;
    //    //GameObject.Find("Main Camera").transform.Translate(vec * Time.deltaTime * 20.0f);
 
    //}
 
    public void ShengC() {
        int i = Random.Range(0, 8);
        if ((i == 0) || (i == 1) || (i == 2) || (i == 3)) {
            flag = false;
 
            cubeClone = (GameObject)Instantiate(box, new Vector3(box.transform.position.x + Random.Range(2, 5), 0, 0) + vec
           , box.transform.rotation);
            vec = cubeClone.transform.position - box.transform.position;
            //GameObject.Find("Main Camera").transform.Translate(vec);
            Camera.main.transform.position = Camera.main.transform.position + vec / 5;
        }
        if ((i == 4) || (i == 5) || (i == 6) || (i == 7)) {
            flag = true;
 
            cubeClone = (GameObject)Instantiate(box, new Vector3(0, 0, box.transform.position.z + Random.Range(2, 5)) + vec
           , box.transform.rotation);
            vec = cubeClone.transform.position - box.transform.position;
            //GameObject.Find("Main Camera").transform.Translate(vec);
            Camera.main.transform.position = Camera.main.transform.position + vec / 5;
 
        }
    }
 
 
}