鱼C论坛

 找回密码
 立即注册
查看: 2464|回复: 2

[原创] 使用unity自制“跳一跳”

[复制链接]
发表于 2018-1-9 22:21:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
主要代码如下:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;

  6. public class TabJump : MonoBehaviour {


  7.     public GameObject cubes;//方块预制体

  8.     public float Force = 500;//力度

  9.     public float DropForce;//按压力

  10.     private Rigidbody rigidbody;//刚体组件

  11.     private Animation animation;//动画组件

  12.     private int score;

  13.     public Text scoretxt;

  14.     public AudioClip jump;
  15.     public AudioClip down;

  16.     private AudioSource ad;


  17.     //private Camera can;

  18.     void Start()
  19.     {
  20.         rigidbody = GetComponent<Rigidbody>();
  21.         animation = GetComponent<Animation>();
  22.         ad=GetComponent<AudioSource>();
  23.         //can = GameObject.Find("Main Camera").gameObject.GetComponent<Camera>();
  24.         scoretxt.text = score.ToString();
  25.     }

  26.     [Header("X轴的偏移量")]
  27.     public float _x = -0.5f;

  28.     private Vector3 nextPos;//下一个生成物体的位置
  29.     private void Update()
  30.     {
  31.         Jump(nextPos);        
  32.     }
  33.     //void OnCollisionEnter(Collision collision)
  34.     void OnCollisionEnter(Collision col)
  35.     {
  36.        if(col.transform.tag=="plane")//如果碰到了plane,重新加载场景
  37.        {
  38.            SceneManager.LoadScene("001");
  39.        }

  40.         if(col.transform.tag=="box")//如果碰到了一个标签为box的并且可以克隆
  41.         {
  42.             creatbox(col.transform.position);//克隆一个方块
  43.             ad.PlayOneShot(down);
  44.             nextPos = col.transform.position+pos;//将碰到的物体的位置赋值给nextPos
  45.             if (canaddscore)
  46.             {
  47.                 score += 1;
  48.             }
  49.             scoretxt.text = score.ToString();
  50.         }        
  51.     }
  52.    
  53.     void UpdateCamera()//跳完相机移动
  54.     {
  55.         Camera.main.transform.localPosition =  new Vector3(0, 0, 2f);
  56.     }
  57.     Vector3 pos = new Vector3(0, 0, 2);//Z轴偏移量为2

  58.     GameObject newcube;//声明一个游戏物体变量
  59.     private bool CanCopy=false;//是否可以克隆
  60.     bool canaddscore;
  61.     void Jump(Vector3 pos)
  62.     {

  63.         Vector3 Dir = (pos - transform.position).normalized;//跳的方向(向量的减法运算)
  64.         
  65.         if (Input.GetMouseButton(0))//鼠标按下
  66.         {
  67.             animation.Play("reset");
  68.             DropForce += 0.05f;//按压力+=0.05
  69.             if (DropForce >= 7.0f)//如果大于等于5
  70.             {
  71.                 DropForce = 7.0f;//就等于5
  72.             }

  73.             if(DropForce>=2.0f)
  74.             {
  75.                 canaddscore = true;
  76.             }
  77.         }

  78.         if (Input.GetMouseButtonUp(0))//鼠标抬起
  79.         {
  80.             CanCopy=true;//将能否克隆开关改为true
  81.             ad.PlayOneShot(jump);
  82.             rigidbody.AddForce(Vector3.up* Force * DropForce/2);//添加一个向上的力
  83.             rigidbody.AddForce(Dir * Force * DropForce/2);//添加一个向下一个物体跳向的力
  84.             animation.Play("jump");//播放翻转动画
  85.             DropForce = 0;//按压力重置为0
  86.         }
  87.     }

  88.     /// <summary>
  89.     /// 克隆一个方块
  90.     /// </summary>
  91.     /// <param name="trans">位置</param>
  92.     void creatbox(Vector3 trans)
  93.     {
  94.         newcube = Instantiate(cubes, trans + pos, Quaternion.identity) as GameObject;
  95.         newcube.name = "box";
  96.     }
  97. }
复制代码


将此代码挂载到玩家身上即可。注意要设置一些东西。如物体的标签,玩家的刚体等,相信unity开发者应该很熟悉了。我已经将工程打包成了unity插件包:如果看不懂的额可以下载来看看哦

JumpJump.zip (861.28 KB, 下载次数: 37)

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-1-9 22:47:55 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-1-11 02:03:22 | 显示全部楼层
看不懂
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-14 02:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表