[unity3d]汽车的开关门动画播放

简介: 做到汽车的开关门,原本我是用代码进行控制,在update函数中只要让他旋转到一定的角度就OK了,但后来发现这样实现的效果并不好,所以就决定用动画来实现开关门的效果。

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


相关文章
|
30天前
Threejs播放模型自带动画
这篇文章介绍了在Three.js中如何播放带有预设动作的模型动画,并特别提到了如何设置动画循环模式以实现一次性播放效果。
35 3
Threejs播放模型自带动画
|
5月前
|
图形学
【unity小技巧】FPS简单的射击换挡瞄准动画控制
【unity小技巧】FPS简单的射击换挡瞄准动画控制
29 0
|
5月前
|
图形学
【unity小技巧】最简单的FPS游戏准心跳动动画控制
【unity小技巧】最简单的FPS游戏准心跳动动画控制
37 0
|
11月前
|
图形学
Unity——音乐、音效
Unity——音乐、音效
An动画基础之元件的影片剪辑动画与传统补间
An动画基础之元件的影片剪辑动画与传统补间
337 0
An动画基础之元件的影片剪辑动画与传统补间
An动画基础之元件的影片剪辑效果
An动画基础之元件的影片剪辑效果
411 0
An动画基础之元件的影片剪辑效果
|
程序员 图形学 开发者
Unity 之游戏特效
Unity 之游戏特效
286 0
Unity 之游戏特效
|
PHP 图形学
Unity Cinemachine & Timeline 制作镜头动画
Unity Cinemachine & Timeline 制作镜头动画
659 0
Unity Cinemachine & Timeline 制作镜头动画
实现场景切换的时候淡入淡出的效果(Unity3D)
在游戏中不可避免的要进行场景切换,如果切换的场景要加载的资源太多太大,那么就要耗时很长时间,这对于用户的体验来说很不舒服,也就是常说的游戏卡了。怎么解决这种尴尬呢。比较推荐的就是异步加载场景,然后中间加一个过渡场景,过渡场景做的美观一些,然后加一个进度条,看起来就不会那么尴尬了。然后在场景过渡的时候,加一个淡入淡出的效果,就很nice了今天就实现一个场景切换的时候淡入淡出的效果。剩下的场景异步加载还有中间过渡场景有时间再写。
|
存储 Web App开发
视频编辑,4k播放,3D游戏, 阿里云图形工作站,了解一下?
设计师小哥哥,小姐姐们,还在用MacBook Pro做视频编辑? 还在一直为长时间的图形渲染占据电脑资源而无休止的等待?还在为大量的素材文件拷贝共享而犯愁? 爱玩游戏的兄弟姐妹们,还在打开电器商城在精挑细选一款适合玩游戏的笔记本? 现在,您无须在本地做视频编辑而担心效率。
2534 0