做到汽车的开关门,原本我是用代码进行控制,在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); } } }