public class RotateAroundAndLookAt : MonoBehaviour {
public GameObject rotateCenter; //旋转中心对象
public float rotateSpeed = 10.0f; //旋转的速度
// Update is called once per frame
void Update () {
if(rotateCenter) //只有旋转中心物体存在时才能进行物体的公转
{
transform.RotateAround(
rotateCenter.transform.position,//旋转的中心点
rotateCenter.transform.up,//旋转轴 这里设置为Y轴朝上
Time.deltaTime * rotateSpeed //旋转的角度 表示每秒转多少度
);
}
}
}
视角切换脚本
public class CameraSwitch : MonoBehaviour {
public Camera mainCamera; //主摄像机
public Camera orthCamera; //正交摄像机
// Use this for initialization
void Start () {
mainCamera.enabled = true;//初始视角为主摄像机
orthCamera.enabled = false;//初始时禁用正交摄像机
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.S))//键盘S来切换视角
{
mainCamera.enabled = !mainCamera.enabled;
orthCamera.enabled = !orthCamera.enabled;
}
}
}
给大球添加作用力脚本
public class ObjectAddForce : MonoBehaviour {
public int force;//作用力大小
// Update is called once per frame
void Update () {
gameObject.GetComponent<Rigidbody>() //获取游戏对象上的刚体组件 取消勾选Rigidbody使用重力选项
.AddForce(new Vector3(0, -force, 0));//添加向下的的作用力即Y轴负方向
}
}
大球自转脚本
public class SelfRotate : MonoBehaviour {
public float rotateSpeed = 40.0f;
// Update is called once per frame
void Update () {
transform.Rotate(Vector3.up, Time.deltaTime * rotateSpeed);
}
}
音效播放脚本
public class DominoCollide : MonoBehaviour {
//当有物体与该物体即将发生碰撞时,调用OnCollisionEnter()函数
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag.Equals("Domino")) //根据碰撞物体的标签来判断该物体是否为多米诺骨牌
GetComponent<AudioSource>().Play(); //获取多米诺骨牌撞击音效的AudioSource组件并播放
}
}