Domino

截图与代码均来自        cousera网络游戏设计与开发专项课程  如有侵权,联系本人立即删除

Domino项目

1.创建游戏对象:预制件的使用

2.物理系统:Unity使用的是PhysX by NVIDA。

刚体、恒定力、碰撞体(物理材质)

3.图形系统:三维物体渲染、光源、摄像机。

网格过滤器决定几何形状、网格渲染器决定表面特征

着色器用于图像渲染,可编辑,不受显卡的固定渲染管线限制,如standard shader、skybox

4.音频系统



5.脚本

5.1 给刚体添加向下的力

using UnityEngine;
using System.Collections;
public class ObjectAddForce : MonoBehaviour {
	public int force;	//作用力大小
	//每隔固定时间执行一次,用于物理模拟
	void FixedUpdate () {
		gameObject.GetComponent<Rigidbody>()		//获取游戏对象上的刚体组件
			.AddForce (new Vector3(0,-force,0));	//给刚体添加方向向下的作用力
	}
}

5.2 物体自转

using UnityEngine;
using System.Collections;
public class SelfRotate : MonoBehaviour {
	public float rotateSpeed = 40.0f;	//旋转速度
	//每帧执行一次:物体自转
	void Update () {
		//物体以世界坐标系的向上方向(正Y轴)方向,以rotateSpeed的速度进行顺时针自转
		//Time.deltaTime表示该帧的执行时间,Time.deltaTime * rotateSpeed表示该帧总共自转的角度
		transform.Rotate (Vector3.up, Time.deltaTime * rotateSpeed);	
	}
}
5.3 物体公转(用于camera)
using UnityEngine;
using System.Collections;
public class RotateAroundAndLookAt : MonoBehaviour {
	public GameObject rotateCenter;		//旋转中心对象
	public float rotateSpeed = 10.0f;	//旋转速度
	//每帧执行一次:物体公转
	void Update () {
		if (rotateCenter) {		//当旋转中心对象设置时才进行物体公转
			transform.RotateAround (	
				rotateCenter.transform.position,	//旋转中心点
				rotateCenter.transform.up, 			//旋转轴:此处设置为为旋转中心的向上方向(正Y轴)
				Time.deltaTime * rotateSpeed		//旋转的角度,rotateSpeed表示旋转的速度,Time.deltaTime表示该帧执行的时间
			);
			transform.LookAt(rotateCenter.transform);	//使游戏对象始终朝向旋转中心
		}
	}
}

5.4 物体碰撞音效

using UnityEngine;
using System.Collections;
public class DominoCollide : MonoBehaviour {
	//当有物体与该物体即将发生碰撞时,调用OnCollisionEnter()函数
	void OnCollisionEnter(Collision collision)	
	{
		if (collision.gameObject.tag.Equals("Domino"))	//根据碰撞物体的标签来判断该物体是否为多米诺骨牌
			GetComponent<AudioSource>().Play();			//获取多米诺骨牌撞击音效的AudioSource组件并播放
	}
}

5.摄像头切换

using UnityEngine;
using System.Collections;
public class CameraSwitch : MonoBehaviour {
	public Camera mainCamera;	//主摄像机
	public Camera orthCamera;	//正交摄像机
	//摄像机状态初始化
	void Start(){
		mainCamera.enabled = true;	//启用主摄像机
		orthCamera.enabled = false;	//禁用正交摄像机
	}

	//每帧调用一次:摄像机切换
	void Update () {
            if (Input.GetKeyDown(KeyCode.S)){	//当玩家按下键盘上的“S”键时
			mainCamera.enabled = !mainCamera.enabled;	//切换主摄像机的启用与禁用状态
			orthCamera.enabled = !orthCamera.enabled;	//切换正交摄像机的启用与禁用状态
        }
	}
}

猜你喜欢

转载自blog.csdn.net/qq_27012963/article/details/80138845