第一个工程3-代码解释一
2016-07-13 23:05
267 查看
在StickController.cs中
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class StickController : MonoBehaviour {
public GameObject whiteBall;
public GameObject tableStick;
public GameObject whiteBallParticleSystem;
private WhiteBallParticleSystemController whiteBallParticleSystemController;
private Vector3 whiteBallOffsetTableStick;
public Vector3 hitVector;
public GameObject hitPowerGameObject;
private Slider hitPower;
public GameObject sceneManagerObject;
private SceneManager sceneManager;
private Rigidbody tableStickRigidBody;
private Rigidbody whiteBallRigidBody;
private Quaternion oldTableStickRotation;
//system configure
private float tableStickRotationSpeed = 50.0f;
private float hitSpeed = 80.0f;
private bool startSlider = false; //空格下上控制状态
private bool leftToRight = true;
private Vector3 tempHitVector;
public bool ifHitReady;
// Use this for initialization
void Start () {
whiteBallOffsetTableStick = tableStick.transform.position - whiteBall.transform.position;
//杆坐标-白球坐标作为两者间的初始偏移,复位用,这里的复位做的比较简单
// Vector3 temp1 = tableStick.transform.TransformPoint (Vector3.forward);
// Vector3 temp2 = tableStick.transform.InverseTransformVector(whiteBallOffsetTableStick);
oldTableStickRotation = tableStick.transform.rotation;
//存储初始球杆的朝向,这是为了基本复位用的
tableStickRigidBody = tableStick.GetComponent<Rigidbody> ();
whiteBallRigidBody = whiteBall.GetComponent<Rigidbody> ();
hitPower = hitPowerGameObject.GetComponent<Slider> ();
sceneManager = sceneManagerObject.GetComponent<SceneManager> ();
whiteBallParticleSystemController = whiteBallParticleSystem.GetComponent<WhiteBallParticleSystemController> ();
ifHitReady = false;
//是否开始击球的标志,false表示未开始击球
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.H)) {
ResetTableStickAndRelated (); //按h停止白球,杆复位
}
float rotation = Input.GetAxis ("Horizontal") * tableStickRotationSpeed * Time.deltaTime;
tableStick.transform.RotateAround (whiteBall.transform.position, Vector3.up, -20 * Time.deltaTime * rotation);
//这两句根据方向键使杆围绕白球转,就是调整击球方向
if (Input.GetKeyDown (KeyCode.Space)) {
startSlider = true;
//按下空格键, startSlider = true力度条变为运动状态
}
if (startSlider == true) {
if (leftToRight) {
hitPower.value++;
if (hitPower.value > hitPower.maxValue - 1)
leftToRight = false;
} else {
hitPower.value--;
if (hitPower.value < hitPower.minValue + 1)
leftToRight = true;
}
} //startSlider == true时,力度条根据帧往复运动
if (Input.GetKeyUp (KeyCode.Space)) {
startSlider = false;
//抬起空格,startSlider = false停止力度条
ifHitReady = true;
//击球该是
hitVector = (whiteBall.transform.position - tableStick.transform.position).normalized;
//计算击球方向
tableStickRigidBody.AddForce (hitVector * hitSpeed, ForceMode.Force);
//对杆施加里,这里杆只是个运动表示,只要最后碰到白球即可,所以力度是固定的,演示用
}
}
public void ResetTableStickAndRelated()
//重置必要内容,显示杆,开始下一杆击球
{
if (sceneManager.resetWhiteBall == true)
sceneManager.ReSetWhiteBall(); //白球需要重置,就将其重置
if (sceneManager.resetBlackBall == true)
sceneManager.ReSetBlackBall();
//黑球需要重置,就将其重置
if (whiteBall.activeSelf == true) {
whiteBallRigidBody.velocity = Vector3.zero;
whiteBallRigidBody.angularVelocity = Vector3.zero;
} //白球没消失,将其停止,这是点H强制停止白球的一个遗留功能
if (tableStick.activeSelf == false) {
tableStick.SetActive (true);
whiteBallParticleSystem.SetActive (true);
} //重新显示杆和击球线,由于击球后我们会让杆消失。
tableStickRigidBody.velocity = Vector3.zero;
tableStick.transform.rotation = oldTableStickRotation;
tableStick.transform.position = whiteBall.transform.position + whiteBallOffsetTableStick;
//简单重置杆的位置
hitPower.value = 0;
//重置击球力
ifHitReady = false;
// ifHitReady = false,上一杆停止
if (sceneManager.CheckIfNoBallIn ())
SceneManager.changePlayer = true;
//检查,没球进换人
if(!sceneManager.CheckIfMyBallIn())
SceneManager.changePlayer = true;
//检查,如果我没有球进换人
if (SceneManager.changePlayer == true) {
sceneManager.ChangePlayer ();
//换人动作
} else
sceneManager.ClearBallInThisTure ();
//不换人,清空本轮进球列表
//把球杆转向该打的最小球的方向
whiteBallParticleSystemController.CurrentPlayerNextBallDirection(whiteBallOffsetTableStick);
//把球杆转向该打的最小球的方向,具体看该代码
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class StickController : MonoBehaviour {
public GameObject whiteBall;
public GameObject tableStick;
public GameObject whiteBallParticleSystem;
private WhiteBallParticleSystemController whiteBallParticleSystemController;
private Vector3 whiteBallOffsetTableStick;
public Vector3 hitVector;
public GameObject hitPowerGameObject;
private Slider hitPower;
public GameObject sceneManagerObject;
private SceneManager sceneManager;
private Rigidbody tableStickRigidBody;
private Rigidbody whiteBallRigidBody;
private Quaternion oldTableStickRotation;
//system configure
private float tableStickRotationSpeed = 50.0f;
private float hitSpeed = 80.0f;
private bool startSlider = false; //空格下上控制状态
private bool leftToRight = true;
private Vector3 tempHitVector;
public bool ifHitReady;
// Use this for initialization
void Start () {
whiteBallOffsetTableStick = tableStick.transform.position - whiteBall.transform.position;
//杆坐标-白球坐标作为两者间的初始偏移,复位用,这里的复位做的比较简单
// Vector3 temp1 = tableStick.transform.TransformPoint (Vector3.forward);
// Vector3 temp2 = tableStick.transform.InverseTransformVector(whiteBallOffsetTableStick);
oldTableStickRotation = tableStick.transform.rotation;
//存储初始球杆的朝向,这是为了基本复位用的
tableStickRigidBody = tableStick.GetComponent<Rigidbody> ();
whiteBallRigidBody = whiteBall.GetComponent<Rigidbody> ();
hitPower = hitPowerGameObject.GetComponent<Slider> ();
sceneManager = sceneManagerObject.GetComponent<SceneManager> ();
whiteBallParticleSystemController = whiteBallParticleSystem.GetComponent<WhiteBallParticleSystemController> ();
ifHitReady = false;
//是否开始击球的标志,false表示未开始击球
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.H)) {
ResetTableStickAndRelated (); //按h停止白球,杆复位
}
float rotation = Input.GetAxis ("Horizontal") * tableStickRotationSpeed * Time.deltaTime;
tableStick.transform.RotateAround (whiteBall.transform.position, Vector3.up, -20 * Time.deltaTime * rotation);
//这两句根据方向键使杆围绕白球转,就是调整击球方向
if (Input.GetKeyDown (KeyCode.Space)) {
startSlider = true;
//按下空格键, startSlider = true力度条变为运动状态
}
if (startSlider == true) {
if (leftToRight) {
hitPower.value++;
if (hitPower.value > hitPower.maxValue - 1)
leftToRight = false;
} else {
hitPower.value--;
if (hitPower.value < hitPower.minValue + 1)
leftToRight = true;
}
} //startSlider == true时,力度条根据帧往复运动
if (Input.GetKeyUp (KeyCode.Space)) {
startSlider = false;
//抬起空格,startSlider = false停止力度条
ifHitReady = true;
//击球该是
hitVector = (whiteBall.transform.position - tableStick.transform.position).normalized;
//计算击球方向
tableStickRigidBody.AddForce (hitVector * hitSpeed, ForceMode.Force);
//对杆施加里,这里杆只是个运动表示,只要最后碰到白球即可,所以力度是固定的,演示用
}
}
public void ResetTableStickAndRelated()
//重置必要内容,显示杆,开始下一杆击球
{
if (sceneManager.resetWhiteBall == true)
sceneManager.ReSetWhiteBall(); //白球需要重置,就将其重置
if (sceneManager.resetBlackBall == true)
sceneManager.ReSetBlackBall();
//黑球需要重置,就将其重置
if (whiteBall.activeSelf == true) {
whiteBallRigidBody.velocity = Vector3.zero;
whiteBallRigidBody.angularVelocity = Vector3.zero;
} //白球没消失,将其停止,这是点H强制停止白球的一个遗留功能
if (tableStick.activeSelf == false) {
tableStick.SetActive (true);
whiteBallParticleSystem.SetActive (true);
} //重新显示杆和击球线,由于击球后我们会让杆消失。
tableStickRigidBody.velocity = Vector3.zero;
tableStick.transform.rotation = oldTableStickRotation;
tableStick.transform.position = whiteBall.transform.position + whiteBallOffsetTableStick;
//简单重置杆的位置
hitPower.value = 0;
//重置击球力
ifHitReady = false;
// ifHitReady = false,上一杆停止
if (sceneManager.CheckIfNoBallIn ())
SceneManager.changePlayer = true;
//检查,没球进换人
if(!sceneManager.CheckIfMyBallIn())
SceneManager.changePlayer = true;
//检查,如果我没有球进换人
if (SceneManager.changePlayer == true) {
sceneManager.ChangePlayer ();
//换人动作
} else
sceneManager.ClearBallInThisTure ();
//不换人,清空本轮进球列表
//把球杆转向该打的最小球的方向
whiteBallParticleSystemController.CurrentPlayerNextBallDirection(whiteBallOffsetTableStick);
//把球杆转向该打的最小球的方向,具体看该代码
}
相关文章推荐
- 我是运营,我没有假期
- 肯特·贝克:改变人生的代码整理魔法
- 你应该学习哪种编程语言?
- 每个 Linux 游戏玩家都绝不想要的恼人体验
- 在 Fedora 上使用 Steam play 和 Proton 来玩 Windows 游戏
- [转]我们需要一种其他人能使用的编程语言
- Steam 让我们在 Linux 上玩 Windows 的游戏更加容易
- 如何使用 Steam Play 在 Linux 上玩仅限 Windows 的游戏
- 新一代iPad适配应用之游戏篇
- DB2编程序技巧(1)
- DB2编程序技巧 (四)
- 女人VS编程_国庆快乐
- DB2编程序技巧 (六)
- DB2编程序技巧 (三)
- DB2编程序技巧 (九)
- DB2编程序技巧 (七)
- DB2编程序小小技巧
- DB2编程序技巧 (五)
- VB实现的《QQ美女找茬游戏》作弊器实例
- DB2编程序技巧 (一)