Unity跳一跳游戏脚本

1.

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

public class Player1 : MonoBehaviour
{
    private Rigidbody m_RigidBody;

    public float fMaxForce = 500.0f;
    private float m_CurForce = 80f;

    public GameObject Box = null;
    public float fMinDistance = 1.2f;//克隆的盒子离预制体盒子的最小距离;
    public float fMaxDistance = 3.0f;//克隆的盒子离预制体盒子的最大距离;
    public float fMinHeiget =0.3f;//克隆的盒子离预制体盒子的最矮
    public float fMaxHeiget = 2.0f;//克隆的盒子离预制体盒子的最高;

    public Vector3 m_Driection = Vector3.forward;//克隆的盒子在前方
    private float m_Distance = 0.0f;//盒子生成的实际距离
    private float m_Height = 0.0f;//盒子生成的实际高度

    private GameObject m_CurCube = null;//当前立方体
    private GameObject m_NextCube = null;//下一个立方体


    private Vector3 m_Camera = Vector3.zero;
    private GameObject m_Plane = null;

    private Animator m_Animator = null;//获取当前动画控制器

    private UIM m_UI = null;
    // Start is called before the first frame update
    void Start()
    {
       
        m_RigidBody = GetComponent<Rigidbody>();
        m_Animator = GetComponent<Animator>();//获取当前动画控制器
        m_UI = GetComponent<UIM>();

        m_Plane = GameObject.FindGameObjectWithTag("Plane");
        m_NextCube = GenerateBox();
    }

    // Update is called once per frame
    void Update()
    {
        GameObject obj = GetObject();
        if (obj != null)
        {
            if (obj.tag == "Cube")//检查是否落在立方体上面
            {
                if (m_CurCube == null) //说明游戏刚刚掉落到立方体上面,游戏刚开始
                {
                    m_CurCube = obj;//游戏刚开始
                    m_Camera = Camera.main.transform.position - m_CurCube.transform.position;//相机坐标减去当前立方体坐标
                }
                else if (m_NextCube==obj)
                {
                    m_UI.AddScore(1);
                    //Destroy(m_CurCube);//3.销毁原来的立方体
                    m_CurCube = m_NextCube;//1.把当前立方体变成下一个立方体,这样不会一直加分
                    m_NextCube = GenerateBox();//2.生成新的立方体
                    m_RigidBody.Sleep();//让刚体休眠一帧,防止物体位移滑动
                   m_RigidBody.WakeUp();//唤醒
                    m_Animator.SetBool("Forward", false);
                    m_Animator.SetBool("Left", false);
                }
                ProcessInput();
                ShowScale();
                CameMove();
            }
            else
            {
                m_UI.SetGameOver(true);
            }
        }
    }

    private void ProcessInput()
    {
        if (Input.GetMouseButtonDown(0))
        {

        }
        else if (Input.GetMouseButton(0))
        {
            m_CurForce += Time.deltaTime * 100;
            if (m_CurForce > fMaxForce)
                m_CurForce = fMaxForce;//限制最大力量
        }
        else if (Input.GetMouseButtonUp(0))
        {
            Jump();
            m_CurForce = 0.0f;//力量清零
        }
 
    }

    private void CameMove()//摄像机跟随主角平滑移动
    {
        Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position,
            m_CurCube.transform.position + m_Camera, Time.deltaTime * 2);


        Vector3 pos = m_CurCube.transform.position;
        pos.y = 0;
        m_Plane.transform.position = pos;
    }
    private void ShowScale() //蓄力表现函数
    {
        float sc =(fMaxForce- m_CurForce*0.5f) / fMaxForce;
        Vector3 scale = transform.localScale;//获取坐标值
        scale.y = sc * 1f;
        transform.localScale = scale;

    }
    private void Jump() //跳高的同时往前跳
    {
        m_RigidBody.AddForce(Vector3.up * m_CurForce);//跳跃高度
        m_RigidBody.AddForce(m_Driection * m_CurForce);//往前跳
        if (m_Driection == Vector3.forward)//播放向前跳跃动画
            m_Animator.SetBool("Forward", true);
        else
            m_Animator.SetBool("Left", true);
    }
    private GameObject GenerateBox() //随机生成盒子
    {
        GameObject obj = GameObject.Instantiate(Box);//创建盒子

        m_Distance = Random.Range(fMinDistance, fMaxDistance);//随机距离
        m_Height = Random.Range(fMinHeiget, fMaxHeiget);//随机大小
        m_Driection = Random.Range(0, 2) == 1 ? Vector3.forward : Vector3.left;//随机方向

        Vector3 pos = m_Driection * m_Distance + transform.position;
        pos.y = fMinHeiget*0.5f;//随机出现在y轴上
        obj.transform.position = pos;

        obj.transform.localScale = new Vector3(1, m_Height, 1);

        obj.GetComponent<MeshRenderer>().material.color = new Color(Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f));
        //更改材质属性里面的颜色,可以生成随机颜色

        return obj;
    }
    private GameObject GetObject()//碰撞物体射线检测
    {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, Vector3.down, out hit, 0.3f))
        {
            //Debug.Log(hit.collider.tag);
            return hit.collider.gameObject;

        }
        else
        {
            Vector3[] vOffests = { Vector3.forward, Vector3.back, Vector3.left, Vector3.right };
            foreach (Vector3 vof in vOffests)
            {
                if (Physics.Raycast(transform.position+vof*0.1f, Vector3.down, out hit, 2f))
                {
                    //Debug.Log(hit.collider.tag);
                    return hit.collider.gameObject;

                }
            }
        }
          return null;
    }
}
 

2.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class UIM : MonoBehaviour
{
    public Text Score;
    public Text BestScore;
    public Text GameOver;

    private int m_nScore = 0;
    private int m_BestScore = 0;
    // Start is called before the first frame update
    void Start()
    {
        GameOver.enabled = false;
        m_nScore = 0;//初始成绩为0
        m_BestScore = PlayerPrefs.GetInt("BestScore");//加载最好成绩
        RefershScorce();
    }

    void RefershScorce()
    {
        string str = string.Format("当前成绩{0}", m_nScore);
        Score.text = str;
         str = string.Format("最好成绩{0}", m_BestScore);
        BestScore.text = str;
    }
    public void AddScore(int sc=1)
    {
        m_nScore += sc;
        if (m_nScore > m_BestScore)
        {
            m_BestScore = m_nScore;
            PlayerPrefs.SetInt("BestScore", m_BestScore);
        }
        RefershScorce();
    }

    public void SetGameOver(bool isEnd=true)
    {
        GameOver.enabled = isEnd;

    }
    // Update is called once per frame
    public void OnReStart()
    {
        SceneManager.LoadScene("Jump and Iump");

    }

    public void OnQuit()
    {
        // Application.Quit();
        SceneManager.LoadScene(0);
    }
}
 

人物控件如图:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Unity跳一跳游戏文件包主要由以下几个部分组成: 1. 游戏场景:游戏场景是游戏的主要背景,也是玩家进行游戏的环境。在Unity中,可以使用Unity编辑器创建和设计游戏场景,通过拖拽和放置不同的游戏对象来建立游戏世界。游戏场景文件通常以.unity文件格式保存。 2. 游戏角色:游戏角色是主要的游戏角色扮演者。在Unity中,可以创建游戏角色的3D模型或2D精灵,并为其添加动画、碰撞体和脚本等组件,从而实现游戏角色的交互和控制。游戏角色的模型文件通常以.unitypackage或.fbx文件格式保存。 3. 游戏物体:游戏物体是游戏中的其他可交互元素,如平台、障碍物、道具等。同样地,在Unity中,可以使用Unity编辑器创建和设计游戏物体,并为其添加相应的组件和属性,以实现其功能和行为。游戏物体的模型文件通常以.unitypackage或.fbx文件格式保存。 4. 材质和纹理:材质和纹理是用来渲染游戏对象的外观和质感的。在Unity中,可以为游戏对象创建不同的材质,并在材质中应用相应的纹理或颜色。材质和纹理文件通常以.unitypackage或.png、.jpeg等图片格式保存。 5. 脚本和代码:脚本和代码是实现游戏逻辑和交互的关键部分。在Unity中,可以使用C#或UnityScript等编程语言编写脚本和代码,来控制游戏对象的移动、碰撞检测、音效播放等行为。脚本和代码文件通常以.cs或.js文件格式保存。 以上是Unity跳一跳游戏文件包的主要组成部分。通过组合和配置这些文件,开发者可以创建出具有各种游戏体验和玩法的跳一跳游戏

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值