unity开发知识点小结02

虚拟轴

虚拟轴就是一个数值在-11内的轴,这个数轴上重要的数值就是-1,0和1。当使用按键模拟一个完整的虚拟轴时需要用到两个按键,即将按键1设置为负轴按键,按键2设置为正轴按键。在没有按下任何按键的时候,虚拟轴的数值为0;在按下按键1的时候,虚拟轴的数值会从0-1进行过渡,而不是直接变成-1,;在按下按键2的时候,虚拟轴的数值会从0~1进行过渡
在这里插入图片描述
通过Project Settings设置轴的数量以及属性等
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AxisText : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        
    }
    void Update()
    {
    
    
        //获取水平轴
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        Debug.Log(horizontal + " " +vertical);
        //虚拟按键
        if (Input.GetButtonDown("Jump")) {
    
    //触发按键
            Debug.Log("空格");
        }
    }
}

通过按下上下左右键可改变其水平,垂直虚拟轴的值
在这里插入图片描述

手机,平板等设备实现触摸操作

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TouchText : MonoBehaviour
{
    
    
    
    void Start()
    {
    
    
        //开启多点触摸
        Input.multiTouchEnabled = true;
    }

    void Update()
    {
    
    
        //判断单点触摸
        if (Input.touchCount ==  1) {
    
    
            //获取触摸对象
            Touch touch = Input.touches[0];
            //获取触摸位置
            Debug.Log(touch.position);
            //获取触摸阶段
            switch (touch.phase)
            {
    
    
                case TouchPhase.Began://开始触摸
                    break;
                case TouchPhase.Moved://触摸移动
                    break;
                case TouchPhase.Stationary://触摸静止
                    break;
                case TouchPhase.Ended://触摸结束
                    break;
                case TouchPhase.Canceled://触摸因为其他事件打断
                    break;
            }

            //判断多点触摸
            if (Input.touchCount == 2) {
    
    
                Touch touch1 = Input.touches[0];//获取第一个触摸点
                Touch touch2 = Input.touches[1];//获取第二个触摸点
            }
        }
    }
}

实时灯光是通过实时计算形成的灯光----性能消耗高
烘焙灯光是当灯光在场景中已经形成相应的效果我们可以把灯光删除,但是场景依然存在灯光效果

在这里插入图片描述
在这里插入图片描述

相机深度,如果场景里面同时存在多个相机,默认显示深度较高的那个相机拍摄到的物体,清除标志如果是仅深度那么就是将各个相机排到的物体叠加显示

通过脚本控制音乐播放

将音频文件放入项目并在物体脚本中设置相应的变量,将音频添加到脚本中
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MusicText : MonoBehaviour
{
    
    
    // 获取需要播放的音频片段
    public AudioClip mode;
    public AudioClip mode2;

    //播放器组件
    private AudioSource player;
    void Start()
    {
    
    
        //获取播放器组件
        player = GetComponent<AudioSource>();
        //设定播放的片段
        player.clip = mode;
        //实现循环播放
        player.loop = true;
        //控制音量
        player.volume = 0.5f;
        //开始播放
        player.Play();
        
    }

    void Update()
    {
    
    
        //按空格实现暂停和继续
        if (Input.GetKeyDown(KeyCode.Space)) {
    
    
            if (player.isPlaying)
            {
    
    
                player.Pause();//暂停
                //player.Stop();//停止

            }
            else {
    
    
                player.UnPause();//继续播放
                //player.Play();//重新播放
            }
        }

        //按下按键播放其他音效
        if (Input.GetMouseButtonDown(0)) {
    
    
            player.PlayOneShot(mode2);//播放一次音效
        }
    }
}

视频播放

先将视频文件导入项目,在场景中穿件一个平面,并且穿件一个渲染器纹理,在将视频文件加到渲染器纹理中,在创建控制视频播放的脚本,将脚本添加到纹理器中。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;

public class VudioText : MonoBehaviour
{
    
    
    private VideoPlayer player;//用于接收视频文件
    void Start()
    {
    
    
        player = GetComponent<VideoPlayer>();//接收视频文件
    }

    void Update()
    {
    
    
        if (Input.GetKeyDown(KeyCode.Space)) {
    
    
            if (player.isPlaying)
            {
    
    
                player.Pause();
            }
            else {
    
    
                player.Play();

            }
        }
    }
}

控制人物移动

在编写脚本之前我们要向人物添加Character Controller组件
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveText : MonoBehaviour
{
    
    
    private CharacterController player;//创建角色控制器
    void Start()
    {
    
    
        player = GetComponent<CharacterController>();

    }

    void Update()
    {
    
    
        //获取水平轴
        float horizontal = Input.GetAxis("Horizontal");
        //获取垂直轴
        float vertical = Input.GetAxis("Vertical");
        //创建成一个方向向量
        Vector3 dir = new Vector3(horizontal,0,vertical);
        //让物体朝该方向移动
        //player.Move(dir);   //该方法不存在重力效果
        player.SimpleMove(dir);
    }
}

碰撞及监听

要实现物体的碰撞效果,首先要将物体都设置碰撞器,此外还要确保碰撞的物体最少其中一个要有设置刚体组件

在这里插入图片描述

首先对碰撞物体进行脚本编写来监听碰撞事件 ,写者编写的碰撞为两物体碰撞之后产生爆炸,碰撞时销毁物体然后产生爆炸效果,一定时间后又将爆炸效果销毁
爆炸预设体挂载脚本
在这里插入图片描述

扫描二维码关注公众号,回复: 14743125 查看本文章
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ColText : MonoBehaviour
{
    
    
    //创建碰撞之后产生效果的预设体
    public GameObject prefad;
    void Start()
    {
    
    
        
    }

    
    void Update()
    {
    
    
        
    }
    //监听发生爆炸    函数所带的元素为和自己发生碰撞的物体
    private void OnCollisionEnter(Collision collision)
    {
    
    
        //产生碰撞创建碰撞效果物体   (创建预设体,设定预设体初始位置设置预设体初始旋转)
        Instantiate(prefad, transform.position, Quaternion.identity);
        //销毁自身
        Destroy(gameObject);
    }

    //持续碰撞中
    private void OnCollisionStay(Collision collision)
    {
    
    

    }

    //结束碰撞
    private void OnCollisionExit(Collision collision)
    {
    
    
        
    }
}

如果要销毁碰撞后产生的爆炸效果应该在单独编写脚本并且将脚本挂载到爆炸效果的预设体上
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ExplorText : MonoBehaviour
{
    
    
    float timer = 0;
    void Start()
    {
    
    
        
    }

    // Update is called once per frame
    void Update()
    {
    
    
        timer += Time.deltaTime;
        if (timer > 1) {
    
     
        Destroy(gameObject);     
        }
    }
}

触发器

当某个物体到达某个位置或者触发到某个触发器之后会触发其他场景物体效果
写者将做一个将目标对象移到固定位置造成固定物体消失
触发器和碰撞的区别,设置触发器物体之间可以穿透,碰撞物体之间不能穿透

设置正方体以及长方体为触发器,胶囊移动到正方体位置长方体消失
在这里插入图片描述

在这里插入图片描述

为胶囊设置移动脚本,见上文
为正方体设置触发事件脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class moveclo : MonoBehaviour
{
    
    
   
    void Start()
    {
    
    
        
    }
    void Update()
    {
    
    
        
    }
    //进入触发器   函数元素代表进入触发区的物体
    private void OnTriggerEnter(Collider other)
    {
    
    
        //若有物体进入触发器,将名称为door的物体设为不显示
        GameObject door = GameObject.Find("door");
        if (door != null) {
    
     
            door.SetActive(false);
        }
    }
    //停留在触发器中
    private void nTriggerStay(Collider other)
    {
    
    
        
    }
    //离开触发器
    private void OnTriggerExit(Collider other)
    {
    
    
        
    }
}

效果展示
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_48627750/article/details/129343394
今日推荐