这是第一人称控制器脚本,如果把摄像机放在胶囊体后面感觉就是第三人称了啊。
#pragma strict var speed : float = 6.0; var jumpspeed: float = 8.0; var gravity : float = 20.0; private var movedirection: Vector3 = Vector3.zero; private var grounded: boolean = false; private var qiqiu: boolean = false; function FixedUpdate () { qiqiu = chufaqiqiu.power_q; if (grounded){ movedirection = Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical")); movedirection = transform.TransformDirection(movedirection); movedirection *= speed; if (Input.GetButton("Jump")){ movedirection.y = jumpspeed; } if (Input.GetKey(KeyCode.Q)) transform.Rotate(0,-25*Time.deltaTime,0,Space.Self); if (Input.GetKey(KeyCode.E)) transform.Rotate(0,25*Time.deltaTime,0,Space.Self); if (Input.GetKey(KeyCode.W) && qiqiu){ movedirection.x *= 2; movedirection.z *= 2; } } movedirection.y -= gravity*Time.deltaTime; var controller: CharacterController = GetComponent(CharacterController); var flags = controller.Move(movedirection*Time.deltaTime); grounded = (flags & CollisionFlags.CollidedBelow) != 0; } @script RequireComponent(CharacterController)
下面是碰撞器脚本
#pragma strict @script RequireComponent(AudioSource) import UnityEngine.UI; var tong_sound: AudioClip; private var pzhuangtime: float = 0.0; private var pzhuang: boolean = false; private var pzhuangobject: GameObject; var pzhuang_time: float = 3.0; var wenzi: Text; /* guang xian tou she peng zhuang var hit: RaycastHit; if (Physics.Raycast(transform.position,transform.forward,hit,3)){ if(hit.collider.gameObject.tag == "hudie"){ var pzhuangobject: GameObject = hit.collider.gameObject; pzhuangobject.SendMessage("mou ge han shu"); */ function Update(){ //peng zhuang ji shi. if(pzhuang){ pzhuangtime += Time.deltaTime; print (pzhuangtime); } } function OnControllerColliderHit(hit: ControllerColliderHit){ /*if(hit.gameObject.tag == "tong"){ hit.gameObject.GetComponent.<AudioSource>().PlayOneShot(tong_sound); }*/ //peng liang ci hu die cai hui xiao shi. if(hit.gameObject.tag == "hudie"){ if(inventory.charge > 4){ pzhuang = true; wenzi.text = "有本事,再碰我一下!"; } else{ wenzi.text = "想抓我,呵呵&……*"; wenzi.enabled = true; } if(pzhuangtime > pzhuang_time){ hit.gameObject.GetComponent.<Animation>().Play("fly"); wenzi.text = "我可以飞的更高SB。"; pzhuangtime = 0.0; pzhuang = false; inventory.charge++; } } }
下面是光线透射脚本。这脚本编译没错,但是功能没有实现。
#pragma strict import UnityEngine.UI; function Updata(){ var hit: RaycastHit; if (Physics.Raycast(transform.position,transform.forward,hit,3)){ if(hit.collider.gameObject.tag == "tong"){ var pzhuangobject: GameObject = hit.collider.gameObject; gameObject.Find("Canvas/tong").GetComponent(Text).text = " 这是一个桶 "; } } }
下面是触发器合集。
#pragma strict import UnityEngine.UI; static var power_t: boolean = false; private var pow_time: float = 0.0; private var speed: float = 0.0; private var wenzi_time: float = 0.0; var wenzi: Text; function Start(){ } function Update(){ if (power_t == true){ pow_time += Time.deltaTime; gameObject.Find("FPControl").GetComponent(fpcyidong).jumpspeed = 16.0; } if (pow_time > 60.0){ power_t = false; gameObject.Find("FPControl").GetComponent(fpcyidong).jumpspeed = 8.0; pow_time = 0.0; } if (wenzi.enabled){ wenzi_time += Time.deltaTime; if (wenzi_time > 10.0){ wenzi.enabled = false; wenzi_time = 0.0; } } } function OnTriggerEnter(col:Collider){ if(col.gameObject.tag == "Player"){ //transform.FindChild("door").SendMessage("dooropen"); //col.gameObject.SendMessage("pickup"); wenzi.text = "这是一个魔力桶,获得60秒超级跳!"; power_t = true ; wenzi.enabled = true; //Destroy(gameObject.transform.parent.gameObject); } }
#pragma strict import UnityEngine.UI; static var power_q: boolean = false; var wenzi: Text; function Start () { } function OnTriggerEnter(col:Collider){ if(col.gameObject.tag == "Player"){ wenzi.text = "这是一个神奇的球,获得终身W冲刺"; power_q = true ; wenzi.enabled = true; } }
#pragma strict import UnityEngine.UI; var wenzi: Text; function OnTriggerEnter(col:Collider){ if(col.gameObject.tag == "Player"){ if(inventory.charge > 5){ wenzi.text = "啊我再飞#¥!"; Destroy(gameObject); Destroy(gameObject.Find("WaterBasicDaytime")); wenzi.enabled = true; } }
#pragma strict import UnityEngine.UI; import UnityEngine.SceneManagement; var wenzi: Text; function OnTriggerEnter(col:Collider){ if(col.gameObject.tag == "Player"){ wenzi.text = "GAME OVER WISH TO NEXT TIME"; Destroy(col.gameObject); wenzi.enabled = true; SceneManager.LoadScene("02"); } }
#pragma strict import UnityEngine.UI; var wenzi: Text; function OnTriggerEnter(col:Collider){ if(col.gameObject.tag == "Player"){ wenzi.text = "GAME OVER"; Destroy(col.gameObject); wenzi.enabled = true; } }
#pragma strict private var chufatime: float = 0.0; private var chufa: boolean = false; private var chufaobject: GameObject; var chufa_time: float = 3.0; var collecthudie: AudioClip; var wenzi : Text; function Update(){ if(chufa){ chufatime += Time.deltaTime; print(chufatime); } } function OnTriggerEnter(col:Collider){ if(col.gameObject.tag == "Player"){ if (inventory.charge > 4){ chufa = true; } else wenzi.text = "bang wo na jige xiang zi lai"; if(chufatime > chufa_time){ gameObject.GetComponent.<Animation>().Play("fly"); AudioSource.PlayClipAtPoint(collecthudie,transform.position); chufatime = 0.0; chufa = false; } } }
下面是旋转和变色脚本
#pragma strict function Update () { transform.Rotate(0,100*Time.deltaTime,0); } function OnTriggerEnter(col:Collider){ if(col.gameObject.tag == "Player"){ //transform.FindChild("door").SendMessage("dooropen"); col.gameObject.SendMessage("pickup"); Destroy(gameObject); } }
#pragma strict var t:float = 0; function Start () { } function Update () { t += Time.deltaTime; if (t <= 2){ GetComponent.<Renderer>().material.color = Color.blue; } else if (t > 2 && t <= 5){ GetComponent.<Renderer>().material.color = Color.red; } else{ GetComponent.<Renderer>().material.color = Color.green; } if (t > 7) t = 0; }
下面是收集物品脚本,另一内建数组var cms:Renderer[]; for(var co:Renderer in cms){co.material.color = Color.red;},给数组内所有对象的 渲染器组件 染色。
#pragma strict import UnityEngine.UI; static var charge: int = 0; var collectSound: AudioClip; var powerui_raw: RawImage; var textures: Texture2D[]; var wenzi: Text; function Start () { charge = 0; } function pickup(){ AudioSource.PlayClipAtPoint(collectSound,transform.position); charge++; wenzi.text = "哎呀获得一个箱子"; powerui_raw.texture = textures[charge]; wenzi.enabled = true; print (charge); }
最后是游戏开始脚本
#pragma strict import UnityEngine.SceneManagement; function OnGUI(){ if(GUI.Button(Rect(180,100,60,30),"退出")){ Application.Quit(); } if(GUI.Button(Rect(280,100,60,30),"开始")){ Application.LoadLevel("01"); } } function Update(){ if (Input.GetKey(KeyCode.Q)) transform.Rotate(0,-25*Time.deltaTime,0,Space.Self); if (Input.GetKey(KeyCode.E)) transform.Rotate(0,25*Time.deltaTime,0,Space.Self); }
loadlevel是能用的但是过时了,用LoadScene