本人录制的一个系列的视频教程,尽量加快的讲解速度,以期节约大家的时间。。。。内容绝对是中文的,,期待大家的回复。

下载链接:http://pan.baidu.com/s/1kTIc8sf  密码:8y3l

代码如下:

1. 摄像头跟随

using UnityEngine;
using System.Collections;public class CameraFollowScript : MonoBehaviour {public GameObject car;public float distance = 5;public float heightDiff = 3;public float angleDamping = 1.5f; // 缓冲系数public float heightDamping = 1f;public float defaultFOV = 60;public float zoomRatio = 1.2f;private float dstAngleWithDirection = 0;// Use this for initializationvoid Start () {} // Update is called once per framevoid Update () {float myH = transform.position.y;float dstH = car.transform.position.y + heightDiff;float retH = Mathf.Lerp(myH, dstH, heightDamping * Time.deltaTime);float myAngle =  transform.eulerAngles.y;float dstAngle = dstAngleWithDirection;float retAngle = Mathf.LerpAngle(myAngle, dstAngle, angleDamping * Time.deltaTime);Quaternion retRotation = Quaternion.Euler(0, retAngle, 0);transform.position = car.transform.position;transform.position -= (retRotation * Vector3.forward * distance);Vector3 temp = transform.position;temp.y = retH;transform.position = temp;transform.LookAt(car.transform);}void FixedUpdate(){Vector3 v = car.rigidbody.velocity;//Vector3 faceDirection = car.transform.forward;//float dot = Vector3.Dot(v, faceDirection); // 点积Vector3 carLocalV = car.transform.InverseTransformDirection(v);float dstAnleY = car.transform.eulerAngles.y;if ( carLocalV.z < -0.01f ) dstAngleWithDirection = dstAnleY + 180;elsedstAngleWithDirection = dstAnleY;float speed = v.magnitude;camera.fieldOfView = defaultFOV + speed * zoomRatio;}
}

中间插上视频列表的截图


2. 汽车的控制

using UnityEngine;
using System.Collections;public class CarMoveController : MonoBehaviour {public WheelCollider wheelFL;public WheelCollider wheelFR;public WheelCollider wheelRL; // rear  leftpublic WheelCollider wheelRR;public Transform wheelFLTransform; public Transform wheelFRTransform; public Transform wheelRLTransform; public Transform wheelRRTransform; public float MAXTORQUE = 50;public float MaxAngleAtLowSpeed = 10f;public float MaxAngleAtHighSpeed = 1f;public float maxBrakeTorque = 100f;public float slipForwardStiff = 0.04f;public float slipSteerStiff = 0.025f;public float initForwardStiff;public float initSteerStiff;// audio controlpublic int gearCount = 4;public float minGearSoundPitch = 1.0f;public float maxGearSoundPitch = 3.0f;public AudioSource brakeAudioSource;public float highSpeed = 50;//privatepublic  float curSpeed;public int curGearLevel;private float initRotationFL = 0f;private float initRotationFR = 0f;private bool braked = false;private float MAX_VILOCITY_FOR_STEER = 40f;private float[] gearLevelSpeed;// Use this for initializationvoid Start () {Vector3 temp = rigidbody.centerOfMass;temp.y -= 0.8f;rigidbody.centerOfMass = temp;initForwardStiff = wheelRL.forwardFriction.stiffness;initSteerStiff = wheelRL.sidewaysFriction.stiffness;SeperateGearSpeedLevel();}private void SeperateGearSpeedLevel(){float gearRange = highSpeed / gearCount;gearLevelSpeed = new float[gearCount + 1];gearLevelSpeed[0] = 0f;for (int i = 0; i < gearCount; i ++){gearLevelSpeed[i + 1] = gearRange * (i + 1);}} void FixedUpdate () {float v = Input.GetAxis("Vertical");float h = Input.GetAxis("Horizontal");curSpeed = rigidbody.velocity.magnitude;if( curSpeed < highSpeed ){wheelRL.motorTorque = MAXTORQUE * v;//力矩wheelRR.motorTorque = MAXTORQUE * v;//力矩}else{wheelRL.motorTorque = 0;//力矩wheelRR.motorTorque = 0;//力矩}float speedFactor = rigidbody.velocity.magnitude / MAX_VILOCITY_FOR_STEER;float steerFacter = Mathf.Lerp(MaxAngleAtLowSpeed, MaxAngleAtHighSpeed, speedFactor);wheelFL.steerAngle = steerFacter * h; // 方向的偏移角度wheelFR.steerAngle = steerFacter * h;HandBrake();}private void HandBrake(){braked = false;if (Input.GetButton("Jump")){braked = true;}if (braked){wheelFL.brakeTorque = maxBrakeTorque;wheelFR.brakeTorque = maxBrakeTorque;wheelRL.motorTorque = 0;wheelRR.motorTorque = 0;SetWheelFrictionStiff(wheelRL, slipForwardStiff, slipSteerStiff);SetWheelFrictionStiff(wheelRR, slipForwardStiff, slipSteerStiff);}else{wheelFL.brakeTorque = 0;wheelFR.brakeTorque = 0;SetWheelFrictionStiff(wheelRL, initForwardStiff, initSteerStiff);SetWheelFrictionStiff(wheelRR, initForwardStiff, initSteerStiff);}}private void SetWheelFrictionStiff(WheelCollider wheelRL, float slipForwardStiff, float slipSteerStiff){WheelFrictionCurve temp = wheelRL.forwardFriction;temp.stiffness = slipForwardStiff;wheelRL.forwardFriction = temp;temp = wheelRL.sidewaysFriction;temp.stiffness = slipSteerStiff;wheelRL.sidewaysFriction = temp;}void Update(){initRotationFL += wheelFL.rpm * 360 * Time.deltaTime/ 60;initRotationFL = Mathf.Repeat(initRotationFL, 360);setFinalRotation(wheelFLTransform, initRotationFL, wheelFL.steerAngle);initRotationFR += wheelFR.rpm * 360 * Time.deltaTime/ 60;initRotationFR = Mathf.Repeat(initRotationFR, 360);setFinalRotation(wheelFRTransform, initRotationFR, wheelFR.steerAngle);//wheelFLTransform.Rotate(, 0, 0);  // round per minute//wheelFRTransform.Rotate(wheelFR.rpm * 360 * Time.deltaTime/ 60, 0, 0);  // round per minutewheelRLTransform.Rotate(wheelRL.rpm * 360 * Time.deltaTime/ 60, 0, 0);  // round per minutewheelRRTransform.Rotate(wheelRR.rpm * 360 * Time.deltaTime/ 60, 0, 0);  // round per minute// relative distance of wheel and car bodySetWheelPos(wheelFRTransform, wheelFR);SetWheelPos(wheelFLTransform, wheelFL);SetWheelPos(wheelRRTransform, wheelRR);SetWheelPos(wheelRLTransform, wheelRL);EngineSound(curSpeed);}private void EngineSound(float curSpeed){//gearLevelSpeedfloat gearMinSpeed = 0;float gearMaxSpeed = 20;for (int i = 0; i < gearLevelSpeed.Length; i ++){if (curSpeed >= gearLevelSpeed[i] && curSpeed < gearLevelSpeed[i + 1] + 2){gearMinSpeed = gearLevelSpeed[i];gearMaxSpeed = gearLevelSpeed[i + 1];curGearLevel = i;break;}}float ratio = (curSpeed - gearMinSpeed) / (gearMaxSpeed - gearMinSpeed);ratio = ratio > 1 ? 1 : ratio;audio.pitch = minGearSoundPitch + ratio * (maxGearSoundPitch - minGearSoundPitch);if (braked && curSpeed > 0.2f){if (!brakeAudioSource.isPlaying)brakeAudioSource.Play();}else{brakeAudioSource.Stop();}}private void SetWheelPos(Transform wheelFRTransform, WheelCollider wheelFR){RaycastHit hitPoint;bool isGrounded = Physics.Raycast(wheelFR.transform.position, -1 * wheelFR.transform.up, out hitPoint,wheelFR.radius + wheelFR.suspensionDistance);if (isGrounded){if ((hitPoint.point - wheelFR.transform.position).sqrMagnitude < 0.16f){wheelFRTransform.position = wheelFR.transform.position;}else{wheelFRTransform.position = hitPoint.point + wheelFR.transform.up * wheelFR.radius;}}else{wheelFRTransform.position = wheelFR.transform.position - wheelFR.transform.up * wheelFR.suspensionDistance;}}    void setFinalRotation(Transform trans, float angle, float yAngle){Vector3 ea = new Vector3(angle, yAngle, 0);trans.localRotation = Quaternion.Euler(ea);}
}

unity3d赛车游戏视频教程相关推荐

  1. unity3d 赛车游戏——复位点检测

    一直没有时间写博客 昨天我的CarWaypoints插件也告一段落了 今年没回家,过年就我一个人 挺无聊的,那就休息一天写几篇博客吧 我的代码可能很少,但是思路很重要 希望不懂的朋友别只copy代码 ...

  2. Unity3D赛车游戏+脚本基础

    前言 游戏对象实例化 Scenes游戏场景 GameObject游戏对象 Component组件 Component使用方法 预制体 Unity事件函数 Instantiate():实例化对象 什么是 ...

  3. 使用Unity3D 自主实战开发的赛车游戏实例,关键点记录 (一)之赛车游戏总体介绍

    我之前一直在做C# 的Winform多媒体软件开发.从去年十一月份开始转型自学Unity3D.发现对这个领域.这个方向更为喜爱.在经过两个多月的跟书自学.跟游戏实例教程练习之后,决定自己完成一款游戏, ...

  4. 使用Unity3D的设计思想实现一个简单的C#赛车游戏场景

    最近看了看一个C#游戏开发的公开课,在该公开课中使用面向对象思想与Unity3D游戏开发思想结合的方式,对一个简单的赛车游戏场景进行了实现.原本在C#中很方便地就可以完成的一个小场景,使用Unity3 ...

  5. 使用Unity3D 自主实战开发的赛车游戏实例

    一.综述 赛车游戏源码下载:http://fanshubbs.com/thread-255-1-1.html 赛车游戏的敌人赛车自动寻路一般有两种方式,一种是路点寻路,另外一种就是使用Unity自带组 ...

  6. Unity3d WheelCollider制作赛车游戏

    真实赛车游戏 车轮碰撞器 车轮碰撞器 首先我们要知道车轮碰撞器里面的一些数据,给大家看一张表: 1.我们可以去资源商店里面找一个车子的模型下载下来,要车身和轮胎是分开的.WheelCollider 2 ...

  7. 无缝切地图的3D赛车游戏火了,小哥花16个月用JS打造,浏览器免费就能玩

    萧箫 发自 凹非寺 量子位 | 公众号 QbitAI 一位小哥耗时16个月打造的3D版赛车游戏,这两天忽然火了起来. 只需一个浏览器,就能驾车从森林.海滩,"无缝切换"到广袤的沙漠 ...

  8. Tile Racer — 3D 赛车游戏

    Toy Posted in Tile Racer 是一款可免用度于 Linux 及 Windows 平台的 3D 赛车游戏.它不只具有十分逼真的效果,并且包罗用来创设新 Maps 的赛道编辑器.玩家可 ...

  9. Unity制作2D动作平台游戏视频教程

    Metroidvania工具包:打造统一的2D行动平台 流派:电子学习| MP4 |视频:h264,1280×720 |音频:AAC,48.0 KHz 语言:英语+中英文字幕(根据原英文字幕机译更准确 ...

  10. Unity Android 2021:用C#打造3D ZigZag赛车游戏

    Unity Android 2021 : Build 3D ZigZag Racing Game with C# MP4 |视频:h264,1280×720 |音频:AAC,44.1 KHz,2 Ch ...

最新文章

  1. PC上安装MAC X Lion
  2. MapReduce改造fp-growth算法
  3. 2020中国社交电商消费者购物行为研究报告
  4. 在线英文名随机生成器
  5. 腾讯手游助手吃鸡一直服务器繁忙,腾讯手游助手里的吃鸡鼠标移动太快怎么办?...
  6. Maven 安装教程详解
  7. QQ互联登录- 前端为 vue.js
  8. P2837 [USACO08FEB]Dining Cows B 题解
  9. SpringBoot + FreeMarker + FlyingSaucer 实现PDF在线预览、打印、下载
  10. 多多情报通:拼多多如何取消满返活动?拼多多满返什么意思?
  11. 关闭tslint检测的简单方法
  12. shiro-基本原理和逻辑配置
  13. 【Kay】Java判断正数和负数个数
  14. VMware安装红旗Linux
  15. N子棋(外加双人对战)详解!推荐!!!
  16. 更改iTunes(macOS下)备份路径至移动硬盘
  17. 基于JavaFX的扫lei小游戏
  18. Linux服务器报错:xx.sh: line 4: $‘\r‘: command not found 解决方法(记录在自己的容器中解决过程)
  19. 找出一个二维数组中的鞍点,即该位置上的的元素,在该行上最大,该列上最小,也可能没有鞍点
  20. 小米上市之后,雷军的下一个千亿业务在哪?

热门文章

  1. bat: 调用WinRAR.exe压缩文件
  2. 【labelme】数据标注工具
  3. 前台CSS颜色代码大全
  4. 一部电影晓生活-韩国
  5. pb文件存储成txt, pbtxt文件
  6. k开头的英文单词计算机专业,带有k的英语单词
  7. 用计算机模拟演示样本均值的抽样,计算机模拟演示文稿.ppt
  8. markdown图床使用小记
  9. 2018华为软件精英大赛
  10. 2019年第十届C/C++ B组蓝桥杯省赛真题