您好,登錄后才能下訂單哦!
做到汽車的開關門,原本我是用代碼進行控制,在update函數中只要讓他旋轉到一定的角度就OK了,但后來發現這樣實現的效果并不好,所以就決定用動畫來實現開關門的效果。
操作步驟:
1.Project->Add Tab->Animation 來創建動畫,然后來創建幀動畫。
2.創建完畢之后,可以給關門的最后一幀添加一個事件,但必須確保事件的函數腳本跟動畫附著在同一個物體上,然后雙擊事件指定事件函數。
說明:由于開門關門就是一個重復的相反的動畫,所以開門和關門就做了一個動畫-開門的動畫,關門只需要倒播就行了。
代碼:
播放關門聲音
using UnityEngine; using System.Collections; public class playMusic : MonoBehaviour { public AudioClip music = null; private GameObject camera; public float musicVolume = 0.6f; private AudioSource music_A = null; // Use this for initialization void Start () { //music = GameObject.Find("closedoor1"); camera = GameObject.Find("Main Camera"); music_A = camera.AddComponent<AudioSource>(); //添加聲音 music_A.clip = music; music_A.volume = musicVolume; } // Update is called once per frame void Update () { } int i = 0; void PlayMusic() { if (i != 0) { music_A.Play(); } i++; i = i % 2; } }
using UnityEngine; using System.Collections; public class opendoor : MonoBehaviour { public GameObject camera; //左門 //private bool isOpenDoor1 = false; //private bool openDoor1 = false; //private bool closeDoor1 = false; ////右門 //private bool isOpenDoor2 = false; //private bool openDoor2 = false; //private bool closeDoor2 = false; ////左后門 //private bool isOpenDoor3 = false; //private bool openDoor3 = false; //private bool closeDoor3 = false; ////右后門 //private bool isOpenDoor4 = false; //private bool openDoor4 = false; //private bool closeDoor4 = false; /* //前蓋 private bool isOpenDoor3 = false; private bool openDoor3 = false; private bool closeDoor3 = false; //后蓋 private bool isOpenDoor4 = false; private bool openDoor4 = false; private bool closeDoor4 = false; */ private int target1 = 0; private int target2 = 0; private int target3 = 0; private int target4 = 0; int flag1 = 0; int flag2 = 0; int flag3 = 0; int flag4 = 0; Ray ray; RaycastHit hitobj; private GameObject door; //左門 private GameObject door2; //右門 private GameObject door3; //左后門 private GameObject door4; //右后門 float animSpeed = 0.5f; /* private GameObject frontGate; //前蓋子 private GameObject behindGate; //后蓋子 */ // Use this for initialization void Start () { camera = GameObject.Find("Main Camera"); /* doorl = GameObject.Find("doorL");//左門 door2 = GameObject.Find("doorR");//右門 frontGate = GameObject.Find("frontGate");//前蓋 behindGate = GameObject.Find("behindGate");//后蓋 */ door = GameObject.Find("doorL1"); //左前門 door.AddComponent<BoxCollider>(); door2 = GameObject.Find("doorR1"); //右前門 door2.AddComponent<BoxCollider>(); door3 = GameObject.Find("doorL2"); //左后門 door3.AddComponent<BoxCollider>(); door4 = GameObject.Find("doorR2"); //右后門 door4.AddComponent<BoxCollider>(); //camera = GameObject.Find("MainCamera"); ////添加聲音 //music_A = camera.AddComponent<AudioSource>(); //music_A.clip = music; //music_A.volume = musicVolume; } //public AudioClip music = null; //public float musicVolume = 0.6f; //private AudioSource music_A = null; float n = 0.06f; int t = 0; // Update is called once per frame void Update() { move(); //畫出射線 ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Input.GetMouseButtonDown(0)) { //鼠標點擊車門 if (Physics.Raycast(ray, out hitobj, 1000)) { print("ddd"); Debug.DrawLine(ray.origin, hitobj.point); //左車門 if (hitobj.collider.name == "doorL1") { print("111"); //開門 if (flag1 == 0) { print("anim"); door.animation["doorFL"].speed = animSpeed * 1.0f; door.animation.Play("doorFL"); //openDoor1 = true; //closeDoor1 = false; } else { if (!door.animation.isPlaying) { door.animation["doorFL"].time = door.animation["doorFL"].length; door.animation["doorFL"].speed = -1.0f * animSpeed; door.animation.Play("doorFL"); } else { door.animation["doorFL"].speed = -1.0f * animSpeed; door.animation.Play("doorFL"); } //music_A.Play(); //if (!door.animation.isPlaying) //{ // print("music"); // music_A.Play(); //} //StartCoroutine(closeDoorVoice()); //closeDoor1 = true; //openDoor1 = false; } flag1++; flag1 %= 2; } //右車門 if (hitobj.collider.name == "doorR1") { print(222); if (flag2 == 0) { door2.animation["doorFR"].speed = animSpeed * 1.0f; door2.animation.Play("doorFR"); //openDoor2 = true; //closeDoor2 = false; } else { if (!door2.animation.isPlaying) { door2.animation["doorFR"].time = door2.animation["doorFR"].length; door2.animation["doorFR"].speed = -1.0f * animSpeed; door2.animation.Play("doorFR"); } else { door2.animation["doorFR"].speed = -1.0f * animSpeed; door2.animation.Play("doorFR"); } //closeDoor2 = true; //openDoor2 = false; } flag2++; flag2 %= 2; } //左后車門 if (hitobj.collider.name == "doorL2") { print(222); if (flag3 == 0) { door3.animation["doorBL"].speed = animSpeed * 1.0f; door3.animation.Play("doorBL"); //openDoor3 = true; //closeDoor3 = false; } else { if (!door3.animation.isPlaying) { door3.animation["doorBL"].time = door3.animation["doorBL"].length; door3.animation["doorBL"].speed = -1.0f * animSpeed; door3.animation.Play("doorBL"); } else { door3.animation["doorBL"].speed = -1.0f * animSpeed; door3.animation.Play("doorBL"); } //closeDoor3 = true; //openDoor3 = false; } flag3++; flag3 %= 2; } //右車門 if (hitobj.collider.name == "doorR2") { print(222); if (flag4 == 0) { door4.animation["doorBR"].speed = animSpeed * 1.0f; door4.animation.Play("doorBR"); //openDoor4 = true; //closeDoor4 = false; } else { if (!door4.animation.isPlaying) { door4.animation["doorBR"].time = door4.animation["doorBR"].length; door4.animation["doorBR"].speed = -1.0f * animSpeed; door4.animation.Play("doorBR"); } else { door4.animation["doorBR"].speed = -1.0f * animSpeed; door4.animation.Play("doorBR"); } //closeDoor4 = true; //openDoor4 = false; } flag4++; flag4 %= 2; } } } //if (openDoor1) //{ // print(11); // //開門 // if (target1 < 45 && !isOpenDoor1) // { // door.transform.Rotate(Vector3.forward, 1); // target1 = target1 + 1; // } // else // { // // move(); // isOpenDoor1 = true; // music_A.Play(); // } //} //if (closeDoor1) //{ // print(00); // //關門 // if (isOpenDoor1 && target1 > 0) // { // door.transform.Rotate(-Vector3.forward, 1); // target1 -= 1; // } // else // { // //music_A.Play(); // isOpenDoor1 = false; // } //} //開右門 //if (openDoor2) //{ // print(11); // //開門 // if (target2 < 45 && !isOpenDoor2) // { // door2.transform.Rotate(Vector3.forward, 1); // target2 = target2 + 1; // } // else // { // // move(); // isOpenDoor2 = true; // music_A.Play(); // } //} //if (closeDoor2) //{ // //關門 // if (isOpenDoor2 && target2 > 0) // { // door2.transform.Rotate(-Vector3.forward, 1); // target2 -= 1; // } // else // { // isOpenDoor2 = false; // } //} ////開左后門 //if (openDoor3) //{ // //開門 // if (target3 < 45 && !isOpenDoor3) // { // door3.transform.Rotate(Vector3.forward, 1); // target3 = target3 + 1; // } // else // { // // move(); // isOpenDoor3 = true; // music_A.Play(); // } //} //if (closeDoor3) //{ // //關門 // if (isOpenDoor3 && target3 > 0) // { // door3.transform.Rotate(-Vector3.forward, 1); // target3 -= 1; // } // else // { // isOpenDoor3 = false; // } //} ////開右門 //if (openDoor4) //{ // //開門 // if (target4 < 45 && !isOpenDoor4) // { // door4.transform.Rotate(Vector3.forward, 1); // target4 = target4 + 1; // } // else // { // // move(); // isOpenDoor4 = true; // music_A.Play(); // } //} //if (closeDoor4) //{ // //關門 // if (isOpenDoor4 && target4 > 0) // { // door4.transform.Rotate(-Vector3.forward, 1); // target4 -= 1; // } // else // { // isOpenDoor4 = false; // } //} } //IEnumerator closeDoorVoice() //{ // yield return new WaitForSeconds(door.animation["doorFL"].time); // //music_A.Play(); //} //void changeSpeed(float target,GameObject obj,float n = 0.3f,int t = 0) //{ // doorl.transform.Rotate(Vector3.forward, n*(++t)); // target1 = 0.5*n*t*t; //} //界面的前后左右上下移動 void move() { //前移 if (Input.GetKey(KeyCode.W)) { camera.transform.Translate(transform.forward); } //后移 if (Input.GetKey(KeyCode.S)) { camera.transform.Translate(-transform.forward); } //左移 if (Input.GetKey(KeyCode.A)) { camera.transform.Translate(-transform.right); } //右移 if (Input.GetKey(KeyCode.D)) { camera.transform.Translate(transform.right); } //上 if (Input.GetKey(KeyCode.Q)) { camera.transform.Translate(transform.up); } //下 if (Input.GetKey(KeyCode.E)) { camera.transform.Translate(-transform.up); } //調整仰視視角 if(Input.GetKey(KeyCode.Z)) { camera.transform.Rotate(Vector3.right); } //調整俯視視角 if(Input.GetKey(KeyCode.C)) { camera.transform.Rotate(-Vector3.right); } } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。