欢迎使用CSDN-markdown编辑器

来源:互联网 发布:机器人单片机 编辑:程序博客网 时间:2024/06/05 15:11

这里写图片描述
using UnityEngine;
using System.Collections;
//此脚本完成 按键与移动

public class key: MonoBehaviour {
string gametime = “00:00:00”;
// Use this for initialization
void Start () {

}
float time = 0;
int second = 0;
int minute = 0;
int hour = 0;
// Update is called once per frame
void Update () {
float z = Input.GetAxis(“Horizontal”); //获取x水平轴
float x = Input.GetAxis(“Vertical”); //获取z竖直轴,但在unity中z为正前方
transform.Translate(transform.forward * x*Time.deltaTime,Space.World); //炮台移动
transform.Rotate(transform.up * z, 60 * Time.deltaTime); //炮台转动

   //下方为计时器,相当于钟表    time += Time.deltaTime;    if (time > 1)    {        second++;        time -= 1;    }    if (second>=60)    {        minute++;        second = 0;    }if (minute >=60)    {        hour++;        minute = 0;    }    if (hour >=24)    {        hour = 0;    }    gametime = string.Format("{0:00}:{1:00}:{2:00}", hour, minute, second);}void OnGUI()     //OnGUI是特殊方法不能改变{    GUIStyle stype = new GUIStyle();    stype.alignment = TextAnchor.MiddleCenter;   //这三行目的是为了时间表居中    GUI.Label(new Rect(105,0, 100, 100), gametime,stype);    if(GUI.Button(new Rect(0, 0, 60, 60), "加速1倍"))    {        Time.timeScale = 1;    }if(GUI.Button(new Rect(65, 0, 60, 60), "加速2倍"))    {        Time.timeScale = 2;    }if(GUI.Button(new Rect(Screen.width - 60, Screen.height - 60, 60, 60), "暂停"))    {        Time.timeScale = 0;    }}

}


using UnityEngine;
using System.Collections;
//此脚本实现炮弹的克隆

public class move : MonoBehaviour {
public GameObject objprofabs;
public Transform bulletshoot;
// Use this for initialization
void Start () {

}
void Test()
{

}

// Update is called once per frame
void Update () {
// 此段注释为下方对应 克隆的炮弹 克隆的位置 旋转的角度
GameObject obj = GameObject.Instantiate(objprofabs, bulletshoot.position,transform.rotation) as GameObject;

   // obj.transform.rotation = transform.rotation;}

}


using UnityEngine;
using System.Collections;
//此脚本实现炮弹的发射

public class Shoot : MonoBehaviour {
public GameObject[] objstarr;
Vector3 starposition;
int speed = 3;
// Use this for initialization
void Start () {
starposition = transform.position;
}

// Update is called once per frame
void Update () {
transform.Translate(Vector3.forward*Time.deltaTime*speed, Space.Self); //Space坐标要与transform的移动方向相反
}
}


using UnityEngine;
using System.Collections;

//此脚本实现宠物跟随 ,在这用方块代替
public class Pet : MonoBehaviour {
public GameObject target; //此处target代表炮台
Vector3 starposition;
// Use this for initialization
void Start () {
starposition = target.transform.forward * -2 + target.transform.up * 2;
}

// Update is called once per frame
void Update () {
starposition = target.transform.forward*- 2 + target.transform.up*2; //使宠物一直位于target的上方2米后方2米
transform.position =Vector3.Lerp(transform.position,target.transform.position+starposition,Time.deltaTime);//此处使宠物产生滑动效果
transform.rotation = target.transform.rotation;//宠物跟随target转动
}
}这里写图片描述

原创粉丝点击