之前完成一个体感接钻石的小游戏,大体过程就是天上掉钻石,用一个物体接住,这个物体是通过kinect来控制的他的前后左右。接到不同的钻石就可以获得不同的得分。
这段代码是下落的位置,下落的游戏物体,还定义了ScoreHit()得分。主要解决海上下落问题,此脚本具有通用性。
using UnityEngine;
using System.Collections.Generic;
public class BallCreator : MonoBehaviour
{
// the ball prefab. @note MUST be attached
public GameObject prefab;
// the object we create the balls near @note MUST be attached.
public Transform where;
// holds the player mapper (used to find out if we have a target player).
public NIPlayerManager m_playerManager;
// the time to create the next ball
//private float m_timeToCreateNextBall;
public float m_timeToCreateNextBall;
// The number of balls the user hit (used for scoring)
private int m_numBallsHit = 0;
// The number of balls created (used for scoring)
// private int m_numBallsCreated;
public int m_numBallsCreated;
// Marks that the user has scored a hit (used for scoring).
//
public void ScoreHit()
{
m_numBallsHit++;
}
// mono-behavior initialization
public void Start () {
m_numBallsHit = 0;
m_numBallsCreated = 0;
m_timeToCreateNextBall = 0;
if(m_playerManager==null)
m_playerManager = FindObjectOfType(typeof(NIPlayerManager)) as NIPlayerManager;
}
// mono-behavior Update is called once per frame
public void Update ()
{
if (Time.time < m_timeToCreateNextBall)
return; // we created a ball very recently, wait.
if (m_playerManager == null)
return; // this means we don't even have a plyer manager.
NISelectedPlayer player = m_playerManager.GetPlayer(0);
if (player == null || player.Valid == false || player.Tracking == false)
return; // this means we don't have a calibrated user
if (SkeletonGuiControl.m_mode == SkeletonGuiControl.SkeletonGUIModes.GUIMode)
return; // we don't throw balls while in GUI mode.
// now we know we should throw a ball. We first figure out where (a random around the
// x axis of the "where" transform and a constant modifier on the y and z).
Vector3 pos = where.position;
pos.x += Random.Range(- 2.0f, 2.0f);
pos.y += 8.0f;
pos.z += 2.1f;
// create the ball
Instantiate(prefab, pos, Quaternion.identity);
m_numBallsCreated++;
// we set the time for the next ball. The time itself depends on how many balls were created
// (the more balls, the less time on average).
float maxTime = 5.0f;
float minTime = 1.0f;
if (m_numBallsCreated > 15)
maxTime = 4.0f;
if (m_numBallsCreated > 30)
maxTime = 3.0f;
if (m_numBallsCreated > 45)
minTime = 0.5f;
if (m_numBallsCreated > 85)
maxTime = 2.0f;
m_timeToCreateNextBall = Time.time + Random.Range(minTime,maxTime);
}
// mono-behavior OnGUI shows the scoring
void OnGUI()
{
if (SkeletonGuiControl.m_mode == SkeletonGuiControl.SkeletonGUIModes.GUIMode)
return; // we don't draw score while in GUI mode.
GUI.Box(new Rect(Screen.width/2 -100, 10, 200, 20), "You Hit " + m_numBallsHit + " balls of " + m_numBallsCreated);
}
}
分享到:
相关推荐
unity游戏源码-逃脱鸟游戏源码Escape Bird Unity游戏源码 , Unity工具 , 完整的项目 , 适合学习和二次开发 。 是整个完整的UnityPackage包 , 直接导入项目里面就行。 C#语言的! C#语言的!! C#语言的!!! 重要的事说三...
第一人称射击游戏----FPSGame.zip------------------------unity4.3.4f1----------源代码 第一人称射击游戏----FPSGame.zip------------------------unity4.3.4f1----------源代码
一个基于Unity3d游戏引擎的体感游戏研究与实现.doc
Unity3D体感游戏物体下落脚本 Posted on 2013年07月10日 by U3d / Unity3D脚本/插件 /被围观 26 次 这段代码是下落的
vuforia-unity-6-0-112.unitypackage
Unity体感插件 ZigFu ZDK 很好用的Unity体感套件。
vuforia-samples-advanced-android-ios-unity-4-2-3.zip官网下载太慢了,放在网盘方便大家下载
Unity日志输出插件,在手机和pc上均有用,能很清晰的看到日志打印,非常好用。 使用这个工具,你可以很容易地在游戏内部检查你的编辑器控制台日志!在移动端同样有效! 支持unity2017.4.29 or 更高版本
OpenNI结合Unity3D进行Kinect体感游戏开发,OpenNI+Unity3D Kinect体感游戏开发
unity游戏-微信跳一跳Unity-JumpJump-master.zip-游戏源码
unity4.3.4f1-----unity3d RPG游戏《外星人》完整源码下载.zip unity4.3.4f1-----unity3d RPG游戏《外星人》完整源码下载.zip
Unity真机调试编辑器(Unity-Logs-Viewer-master),兼容iOS和Andriod系统边玩游戏面板显示Debug信息,非常方便;
vuforia-samples-core-unity-6-2-6 These samples show how to build apps using the following core features of Vuforia. Image Targets VuMark Object Recognition Cylinder Targets Multi Targets User ...
为了速度慢下载失败或者无法下载的人提供,这是最新的vuforia unity sdk 6.2.10
Unity和C#-游戏开发-官方超好画质的射击游戏源码+源代码工程
vuforia-unity-6-0-117.unitypackage 链接地址: https://developer.vuforia.com/downloads/sdk#downloadModal 有时候下载速度比较忙,所以上传一份
dnspy所需的mono.dll
Unity和C#-游戏开发-国王卡牌游戏+源代码工程
Unity游戏代码 ,Unity经典游戏源码 , Unity休闲游戏源码, Unity容易上手的游戏源码,Unity小游戏源码,Unity新手练习源码,Unity个人开发源码,Unity优秀项目,Unity商店优秀作品。 如果运行或测试时遇到问题,可随时联系...