1,先把虚拟相机导入后,创建一个相机,按照图上设置。
2,将主角拖入虚拟相机并且设置好他的角度
3,代码展示,此处我写了移动,协程调用时间(震动几秒),碰撞时震动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using Cinemachine;//虚拟相机命名空间
public class Player : MonoBehaviour
{
public CinemachineVirtualCamera camer;//虚拟相机
public CinemachineBasicMultiChannelPerlin perlin;//相机组件
public Coroutine coroutine;//协程
public int speed;//设置速度
// Start is called before the first frame update
void Start()
{
camer = GameObject.Find("CMvcam").GetComponent<CinemachineVirtualCamera>();//找到对应的虚拟相机
perlin = camer.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();//组件获取方便赋值
speed = 1;
}
//协程函数间隔调用
IEnumerator Timer(int timer)
{
int count = 0;//计数
while (true)
{
yield return new WaitForSeconds(timer);//等待timer后调用
count++;
if (count > 1)
{
perlin.m_AmplitudeGain = 0;//振幅
perlin.m_FrequencyGain = 100;//频率
StopCoroutine(coroutine);
count = 0;//重置
}
}
}
//碰撞后产生效果
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Monster")//设置标签
{
perlin.m_AmplitudeGain = 5;//设置属性
perlin.m_FrequencyGain = 100;
coroutine = StartCoroutine(Timer(1));//开始协程
Destroy(collision.gameObject);//删除怪物
}
}
//加速按钮
public void Accelerate()
{
speed = 9;
}
// Update is called once per frame
void Update()
{
//右键点击
if (Input.GetMouseButtonDown(1))
{
//射线检测
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit))
{
GetComponent<NavMeshAgent>().SetDestination(hit.point);//设置目标点
GetComponent<NavMeshAgent>().speed = speed;//设置速度
}
}
}
}
4,效果 我碰到球球后会有震动