个人主页:@元宇宙-秩沅
hallo 欢迎 点赞 收藏⭐ 留言 加关注✅!
本文由 秩沅 原创
收录于专栏:Unity游戏demo
–
️版本: Unity2021
️适合人群:Unity初学者进阶
️学习目标:3D赛车游戏的基础制作
️技能掌握:
Unity3D游戏制作片段【一】
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 汽车特效管理
//___________创建者:_____梅西湖_____
//_____________________________________
//-------------------------------------
public class CarEffect : MonoBehaviour
{
public ParticleSystem[] smoke;
public CarMoveControl carControl;
void Awake()
{
smoke = new ParticleSystem[4];
//给有烟雾数组初始化赋值
for (int i = 0; i < 4; i++)
{
smoke[i] = transform.GetChild(1).GetChild(i).GetComponent<ParticleSystem>();
}
//初始化CarMoveContorl脚本
carControl = gameObject .GetComponent<CarMoveControl>();
StopSmoke();
}
private void Update()
{
//如果烟雾判断开关开启-按下空格或者 烟雾开关开启
if(carControl.playPauseSmoke || InputManager.InputManagerment.handbanl ) StarSmoke();
else StopSmoke();
}
//烟雾特效开始
public void StarSmoke()
{
for (int i = 0; i < 4; i++)
{
smoke[i].Play();
}
}
//烟雾特效停止
public void StopSmoke()
{
for (int i = 0; i < 4; i++)
{
smoke[i].Stop();
}
}
}
WheelHit.sidewaysSlip
WheelHit
//烟雾特效开始
public void StarSmoke()
{
for (int i = 0; i < 4; i++)
{
//var自动变成需要的类型,“你变我也变”
var emisiion = smoke[i].emission;
emisiion.rateOverTime = carControl.Km_H * 10 >= 200 ? carControl.Km_H * 10 : 2000;
smoke[i].Play();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 汽车特效管理
//___________创建者:_____梅西湖_____
//_____________________________________
//-------------------------------------
public class CarEffect : MonoBehaviour
{
public ParticleSystem[] smoke;
public CarMoveControl carControl;
void Awake()
{
smoke = new ParticleSystem[4];
//给有烟雾数组初始化赋值
for (int i = 0; i < 4; i++)
{
smoke[i] = transform.GetChild(1).GetChild(i).GetComponent<ParticleSystem>();
}
//初始化CarMoveContorl脚本
carControl = gameObject .GetComponent<CarMoveControl>();
StopSmoke();
}
private void Update()
{
//如果烟雾判断开关开启-按下空格或者 烟雾开关开启
if(carControl.playPauseSmoke || InputManager.InputManagerment.handbanl ) StarSmoke();
else StopSmoke();
}
//烟雾特效开始
public void StarSmoke()
{
for (int i = 0; i < 4; i++)
{
//var自动变成需要的类型
var emisiion = smoke[i].emission;
emisiion.rateOverTime = carControl.Km_H * 10 >= 200 ? carControl.Km_H * 10 : 2000;
smoke[i].Play();
}
}
//烟雾特效停止
public void StopSmoke()
{
for (int i = 0; i < 4; i++)
{
smoke[i].Stop();
}
}
}
生活中有这样一个有趣的现象:当声音由远及近或者由近及远时,我们会听到声音音调有高低的变化。这个现象就是“多普勒效应”。距离远的时候听到的声音比较低沉,距离近的时候,声音高频成分增加,听起来音调有所上升。
using System;
using UnityEngine;
using Random = UnityEngine.Random;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 汽车声道管理
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class CarAudio : MonoBehaviour
{
//这个脚本读取汽车的一些当前属性,并相应地播放声音。
//发动机的声音可以是一个简单的循环和音调剪辑,或者
//可以是代表引擎音色的四个剪辑的交叉混合
//在不同的转速和节气门状态下。
//发动机夹子都应该是稳定的俯仰,而不是上升或下降。
//当使用四通道发动机交叉火力时,四个夹子应为:
//lowAccelClip:发动机处于低转速,节气门打开(即在非常低的速度下开始加速)
//highAccelClip:高转速下的Thenengine,节气门打开(即加速,但几乎处于最高速度)
//lowDecelClip:发动机处于低转速,节气门处于最小值(即怠速或发动机在极低转速下制动)
//highDecelClip:高转速下的Thenengine,节气门处于最小值(即发动机在非常高的速度下制动)
//为了获得适当的交叉火力,剪辑的音高应该全部匹配,在低音和高音之间有一个八度偏移。
public enum EngineAudioOptions //发动机音频选项
{
Simple, //单声道音频
FourChannel //四声道音频
}
public EngineAudioOptions engineSoundStyle = EngineAudioOptions.FourChannel;//将默认音频选项设置为四声道
public AudioClip lowAccelClip; //用于低加速的音频
public AudioClip lowDecelClip; //用于低减速音频
public AudioClip highAccelClip; //用于高加速的音频
public AudioClip highDecelClip; //用于高减速的音频
public float pitchMultiplier = 1f; // 用于更改音频片段的音调
public float lowPitchMin = 1f; // 低音的最低音高
public float lowPitchMax = 6f; // 低音的最高音高
public float highPitchMultiplier = 0.25f; // 用于改变高音的音高
public float maxRolloffDistance = 500; // 开始发生翻滚的最大距离
public float dopplerLevel = 1; // 音频中使用的多普勒效应
public bool useDoppler = true; // 多普勒效应是否勾选
private AudioSource m_LowAccel; // 低加速度声音的来源
private AudioSource m_LowDecel; // 低减速声音的来源
private AudioSource m_HighAccel; // 高加速度声音的来源
private AudioSource m_HighDecel; // 高减速度声音的来源
private bool m_StartedSound; // 是否已开始发声的标志
//private CarController m_CarController; // Reference to car we are controlling
public CarMoveControl m_CarController;
// private AIcontroller aicontroler;
//开始发声
private void StartSound()
{
//获取carcontroller(这不会为null,因为我们需要组件)
//m_CarController=GetComponent<CarController>();
//设置单声道音频源
m_HighAccel = SetUpEngineAudioSource(highAccelClip);
// 如果枚举选择了四声道音频,设置四个音频源
if (engineSoundStyle == EngineAudioOptions.FourChannel)
{
m_LowAccel = SetUpEngineAudioSource(lowAccelClip);
m_LowDecel = SetUpEngineAudioSource(lowDecelClip);
m_HighDecel = SetUpEngineAudioSource(highDecelClip);
}
//已开始发声的标志
m_StartedSound = true;
}
//停止发声
private void StopSound()
{
//销毁所有音频
foreach (var source in GetComponents<AudioSource>())
{
Destroy(source);
}
//发声停止
m_StartedSound = false;
}
private void FixedUpdate()
{
//获取到主摄像头的距离
float camDist = (Camera.main.transform.position - transform.position).sqrMagnitude; //sqrMagnitude为单位平方毫米
//如果物体和相机的距离 超过最大滚降距离,则停止发声——模拟汽车事故损毁
if (m_StartedSound && camDist > maxRolloffDistance * maxRolloffDistance)
{
StopSound();
}
//如果没有播放,相机的距离和最大距离较近,则启动声音
if (!m_StartedSound && camDist < maxRolloffDistance * maxRolloffDistance)
{
StartSound();
}
if (m_StartedSound)
{
//根据汽车的转速,音高在最小值和最大值之间插值。.
float pitch = ULerp(lowPitchMin, lowPitchMax, m_CarController.engineRPM / m_CarController.maxRPM);
//取最小音高
pitch = Mathf.Min(lowPitchMax, pitch);
if (engineSoundStyle == EngineAudioOptions.Simple)
{
// 单声道时, 高加速度声音的音频资源的音高
m_HighAccel.pitch = pitch * pitchMultiplier * highPitchMultiplier;
m_HighAccel.dopplerLevel = useDoppler ? dopplerLevel : 0;
m_HighAccel.volume = 1;
}
else
{
//4声道引擎声音
//根据乘数调整音高
m_LowAccel.pitch = pitch * pitchMultiplier;
m_LowDecel.pitch = pitch * pitchMultiplier;
m_HighAccel.pitch = pitch * highPitchMultiplier * pitchMultiplier;
m_HighDecel.pitch = pitch * highPitchMultiplier * pitchMultiplier;
float accFade = 0;
// 获取基于加速度的声音衰减值
accFade = Mathf.Abs((InputManager.InputManagerment .vertical > 0 ? InputManager.InputManagerment.vertical : 0));
float decFade = 1 - accFade;
//根据汽车转速获得高衰减值
float highFade = Mathf.InverseLerp(0.2f, 0.8f, m_CarController.engineRPM / 10000);
float lowFade = 1 - highFade;
// 调整一组值以使其更加现实
highFade = 1 - ((1 - highFade) * (1 - highFade));
lowFade = 1 - ((1 - lowFade) * (1 - lowFade));
accFade = 1 - ((1 - accFade) * (1 - accFade));
decFade = 1 - ((1 - decFade) * (1 - decFade));
//根据淡入度值修改音频资源
m_LowAccel.volume = lowFade * accFade;
m_LowDecel.volume = lowFade * decFade;
m_HighAccel.volume = highFade * accFade;
m_HighDecel.volume = highFade * decFade;
//调整多普勒水平值
m_HighAccel.dopplerLevel = useDoppler ? dopplerLevel : 0;
m_LowAccel.dopplerLevel = useDoppler ? dopplerLevel : 0;
m_HighDecel.dopplerLevel = useDoppler ? dopplerLevel : 0;
m_LowDecel.dopplerLevel = useDoppler ? dopplerLevel : 0;
}
}
}
//设置新的音频源并将其添加到 对象
private AudioSource SetUpEngineAudioSource(AudioClip clip)
{
// 在游戏对象上创建新的音频源组件并设置其属性
AudioSource source = gameObject.AddComponent<AudioSource>();
source.clip = clip;
source.volume = 0;
source.spatialBlend = 1;
source.loop = true;
// 音频片段从随机点开始
source.time = Random.Range(0f, clip.length);
source.Play();
source.minDistance = 5;
source.maxDistance = maxRolloffDistance;
source.dopplerLevel = 0;
return source;
}
// 反转Lerp,允许值超过从a到b的范围
private static float ULerp(float from, float to, float value)
{
return (1.0f - value) * from + value * to;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 汽车特效管理
//___________创建者:_____梅西湖_____
//_____________________________________
//-------------------------------------
public class CarEffect : MonoBehaviour
{
public ParticleSystem[] smoke; //获取粒子特效数组
public TrailRenderer[] TrailArray; //获取漂移拖尾数组
public CarMoveControl carControl; //获取脚本CarMoveContorl
void Awake()
{
//初始化两个数组
smoke = new ParticleSystem[4];
TrailArray = new TrailRenderer[4];
//给有烟雾数组 和 拖尾数组 初始化赋值
for (int i = 0; i < 4; i++)
{
smoke[i] = transform.GetChild(1).GetChild(i).GetComponent<ParticleSystem>();
TrailArray[i] = transform.GetChild(1).GetChild(i).GetComponent<TrailRenderer>();
}
//初始化CarMoveContorl脚本
carControl = gameObject .GetComponent<CarMoveControl>();
//特效先停止
StopSmoke();
}
private void Update()
{
//如果烟雾判断开关开启-按下空格或者 烟雾开关开启 和 漂移轨迹拖尾判断
if (carControl.playPauseSmoke || InputManager.InputManagerment.handbanl)
{
StarSmoke(); //开启烟雾
StartTrail(); //开启拖尾特效
}
else
{
StopSmoke(); //关闭烟雾
StopTrail(); //关闭拖尾特效
}
}
//烟雾特效开始
public void StarSmoke()
{
for (int i = 0; i < 4; i++)
{
//var自动变成需要的类型
var emisiion = smoke[i].emission;
emisiion.rateOverTime = carControl.Km_H * 10 >= 200 ? carControl.Km_H * 10 : 2000;
smoke[i].Play();
}
}
//烟雾特效停止
public void StopSmoke()
{
for (int i = 0; i < 4; i++)
{
smoke[i].Stop();
}
}
//拖尾轨迹开启
public void StartTrail()
{
for (int i = 0; i < 4; i++)
{
TrailArray[i].emitting = true;
}
}
//拖尾轨迹关闭
public void StopTrail()
{
for (int i = 0; i < 4; i++)
{
TrailArray[i].emitting = false;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 汽车特效管理
//___________创建者:_____梅西湖_____
//_____________________________________
//-------------------------------------
public class CarEffect : MonoBehaviour
{
public ParticleSystem[] smoke; //获取粒子特效数组
public TrailRenderer[] TrailArray; //获取漂移拖尾数组
public CarMoveControl carControl; //获取脚本CarMoveContorl
public AudioSource skipClip; //获取音频文件
void Awake()
{
//初始化两个数组
smoke = new ParticleSystem[4];
TrailArray = new TrailRenderer[4];
//获取音频资源
skipClip = GetComponent<AudioSource>();
//给有烟雾数组 和 拖尾数组 初始化赋值
for (int i = 0; i < 4; i++)
{
smoke[i] = transform.GetChild(1).GetChild(i).GetComponent<ParticleSystem>();
TrailArray[i] = transform.GetChild(1).GetChild(i).GetComponent<TrailRenderer>();
}
//初始化CarMoveContorl脚本
carControl = gameObject .GetComponent<CarMoveControl>();
//特效先停止
StopSmoke();
//音效先停止
skipClip.Stop();
}
private void Update()
{
//如果烟雾判断开关开启-按下空格或者 烟雾开关开启 和 漂移轨迹拖尾判断
if (carControl.playPauseSmoke || InputManager.InputManagerment.handbanl)
StarSmoke(); //开启烟雾
else
StopSmoke(); //关闭烟雾
//当按下空格时
if (InputManager.InputManagerment.handbanl)
StartTrail(); //开启拖尾特效
else
StopTrail(); //关闭拖尾特效
}
//烟雾特效开始
public void StarSmoke()
{
for (int i = 0; i < 4; i++)
{
//var自动变成需要的类型
var emisiion = smoke[i].emission;
emisiion.rateOverTime = carControl.Km_H * 10 >= 200 ? carControl.Km_H * 10 : 2000;
smoke[i].Play();
}
}
//烟雾特效停止
public void StopSmoke()
{
for (int i = 0; i < 4; i++)
{
smoke[i].Stop();
}
}
//拖尾轨迹开启
public void StartTrail()
{
for (int i = 0; i < 4; i++)
{
TrailArray[i].emitting = true;
}
skipClip.Play();
}
//拖尾轨迹关闭
public void StopTrail()
{
for (int i = 0; i < 4; i++)
{
TrailArray[i].emitting = false;
}
skipClip.Stop();
}
}
⭐【Unityc#专题篇】之c#进阶篇】
⭐【Unityc#专题篇】之c#核心篇】
⭐【Unityc#专题篇】之c#基础篇】
⭐【Unity-c#专题篇】之c#入门篇】
⭐【Unityc#专题篇】—进阶章题单实践练习
⭐【Unityc#专题篇】—基础章题单实践练习
⭐【Unityc#专题篇】—核心章题单实践练习
你们的点赞 收藏⭐ 留言 关注✅是我持续创作,输出优质内容的最大动力!、