記事ディレクトリ
序文
-
もうすぐ今学期が終わり、Unity にプロジェクトを依頼され、毎週試験が行われ、その夜に恋に落ちました。. . . . . . . . . . . . . . .
-
車のシミュレーションをしました、うーん...かろうじて数えます
-
全6シーン
3.1 ログイン・登録シーン
3.2 ローディングシーン
3.3 選択シーン
3.4 迷路マップシーン
3.5 夜道シーン
3.6 被験者2シミュレーションシーン -
車のモデルと夜のシーンはストアで無料で見つけることができます.迷路と主題2のシミュレーションが一緒に構築されています.非常にシンプルで目に見えない.
-
一部の機能は、オンラインで断片的に学習してから、自分で調べます。
-
一般的に言えば、最初は本当に死にたかったのですが、次第に慣れてきて、それでも死にたいと思っていました。
-
結局、それはかなり... 良いものでした。
画面録画
自動車シミュレーション
ワンログイン登録シーン
パスワードログインパネル、アカウント登録パネル、設定パネルコードがあります。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LoginTest : MonoBehaviour
{
public InputField user;
public InputField pass;
public InputField r_User;
public InputField r_Pass;
public InputField r_CPass;
public Text text;
public GameObject registerPanel;
public GameObject settingPanel;
public Toggle toggle;
public Slider slider;
string rUser = "";
string rPass = "";
string rCPass = "";
AudioSource au;
// Start is called before the first frame update
void Start()
{
user = user.GetComponent<InputField>();
pass = pass.GetComponent<InputField>();
text = text.GetComponent<Text>();
r_User = r_User.GetComponent<InputField>();
r_CPass = r_CPass.GetComponent<InputField>();
r_Pass = r_Pass.GetComponent<InputField>();
toggle = toggle.GetComponent<Toggle>();
slider = slider.GetComponent<Slider>();
au = GetComponent<AudioSource>();
//user:123456
//pass:888888
}
public void ActiveSettingPanel()
{
settingPanel.SetActive(true);
}
public void CloseSettingPanel()
{
settingPanel.SetActive(false);
}
public void ActiveRegisterPanel()
{
registerPanel.SetActive(true);
r_User.text = "";
r_Pass.text = "";
r_CPass.text = "";
}
public void DisActiveRegisterPanel()
{
rUser = r_User.text;
rPass = r_Pass.text;
rCPass = r_CPass.text;
if (rUser!=""&&rPass==rCPass&&rPass!=null)
{
registerPanel.SetActive(false);
}
else
{
r_User.text = "";
r_Pass.text = "";
r_CPass.text = "";
}
}
public void CloseRegisterPanel()
{
registerPanel.SetActive(false);
}
public void OnLogin()
{
if (user.text ==rUser&&pass.text==rPass&&pass.text==rCPass&&rPass!="")
{
print("登录成功");
text.text = "账号密码正确,登录成功";
Invoke("ClearFailText", 1);
text.color = Color.green;
Invoke("GotoLoadingScene",1);
}
else
{
print("登录失败");
user.text = "";
pass.text = "";
text.text = "账号或密码错误,登录失败";
Invoke("ClearFailText",1);
text.color = Color.red;
}
}
public void ClearFailText()
{
text.text = "";
}
public void GotoLoadingScene()
{
UnityEngine.SceneManagement.SceneManager.LoadScene(1);//跳转场景
}
// Update is called once per frame
void Update()
{
au.mute = toggle.isOn;
au.volume = slider.value;
}
}
2 つのロード シーン
コード:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnterLoading : MonoBehaviour
{
float t = 0;
Slider slider;
public Text text;
// Start is called before the first frame update
void Start()
{
slider = GetComponent<Slider>();
text = text.GetComponent<Text>();
text.text = "";
slider.value = 0;
}
// Update is called once per frame
void Update()
{
t += Time.deltaTime;
slider.value = t * 10;
text.text = (int)slider.value + "%";
if (t >= 10)
{
slider.value = 100;
text.text = 100 + "%";
UnityEngine.SceneManagement.SceneManager.LoadScene(2);
}
}
}
三択シーン
シーン遷移と遷移効果コード:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class SceneLoad : MonoBehaviour
{
public Image image_Effect;
public float speed = 1;
void Start()
{
StartCoroutine(SceneLoadIn());
}
public void OnClick_Btn_LoadScene_02(string SceneName)
{
StartCoroutine(SceneLoadOut(SceneName));
}
//淡出
IEnumerator SceneLoadOut(string SceneName)
{
Color tempColor = image_Effect.color;
tempColor.a = 0;
image_Effect.color = tempColor;
while (image_Effect.color.a < 1)
{
//Time.deltaTime 指的是当前这一帧。
image_Effect.color += new Color(0, 0, 0, speed * Time.deltaTime);
yield return null;
}
SceneManager.LoadScene(SceneName);
}
//淡入
IEnumerator SceneLoadIn()
{
Color temColor = image_Effect.color;
temColor.a = 0;
image_Effect.color = temColor;
while (image_Effect.color.a > 0)
{
image_Effect.color += new Color(0, 0, 0, -speed * Time.deltaTime);
yield return null;
}
}
}
ラウンドアバウト シーン
- カメラは車のコードに従います
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
[SerializeField] private Vector3 offset;
[SerializeField] private Transform target;
[SerializeField] private float translateSpeed;
[SerializeField] private float rotationSpeed;
private void FixedUpdate()
{
HandleTranslation();
HandleRotation();
}
private void HandleTranslation()
{
var targetPosition = target.TransformPoint(offset);
transform.position = Vector3.Lerp(transform.position, targetPosition, translateSpeed * Time.deltaTime);
}
private void HandleRotation()
{
var direction = target.position - transform.position;
var rotation = Quaternion.LookRotation(direction, Vector3.up);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
}
}
- 車の加速やブレーキなどのサウンド コード:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioManage : MonoBehaviour
{
AudioSource au;
public AudioClip[] clip = new AudioClip[4];
// Start is called before the first frame update
void Start()
{
au = GetComponent<AudioSource>();
}
public void PlayAudio(int num)
{
au.clip = clip[num];
au.Play();
}
// Update is called once per frame
void Update()
{
}
}
- 車の動きの制御、車のライト効果、加速効果、その他のコード
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarController1 : MonoBehaviour
{
AudioManage audioManage;
[SerializeField] public GameObject text;
public GameObject lig;
int li=0;
[SerializeField] public GameObject effect1;
[SerializeField] public GameObject effect2;
private const string HORIZONTAL = "Horizontal";
private const string VERTICAL = "Vertical";
private float HorizontalInput;
private float verticalInput;
private float currentSteerAngle;
private float currentbreakForce;
private bool isBreaking;
[SerializeField] private float motorForce;
[SerializeField] private float breakForce;
[SerializeField] private float maxSteerAngle;
[SerializeField] private WheelCollider frontLeftWheelCollider;
[SerializeField] private WheelCollider frontRightWheelCollider;
[SerializeField] private WheelCollider rearLeftWheelCollider;
[SerializeField] private WheelCollider rearRightWheelCollider;
[SerializeField] private Transform frontLeftWheelTransform;
[SerializeField] private Transform frontRightWheelTransform;
[SerializeField] private Transform rearLeftWheelTransform;
[SerializeField] private Transform rearRightWheelTransform;
void Start()
{
audioManage = GameObject.Find("AudioManage").GetComponent<AudioManage>();
}
private void FixedUpdate()
{
//Time.timeScale = 1.0f;
//text.SetActive(false);
GetInput();
HandleMotor();
HandleSteering();
UpdateWheels();
}
private void GetInput()
{
HorizontalInput = Input.GetAxis(HORIZONTAL);
verticalInput = Input.GetAxis(VERTICAL);
isBreaking = Input.GetKey(KeyCode.Space);
//isShift = Input.GetKey(KeyCode.LeftShift);
}
private void HandleMotor()
{
frontLeftWheelCollider.motorTorque = verticalInput * motorForce;
frontRightWheelCollider.motorTorque = verticalInput * motorForce;
if (Input.GetKeyDown(KeyCode.LeftShift))
{
audioManage.PlayAudio(0);
effect1.SetActive(true);
effect2.SetActive(true);
frontLeftWheelCollider.motorTorque = 20000000.0f;
frontRightWheelCollider.motorTorque = 20000000.0f;
}
if (Input.GetKey(KeyCode.L))
{
if (li == 0)
{
lig.SetActive(true);
li = 1;
}
else
{
lig.SetActive(false);
li = 0;
}
}
if (Input.GetKeyDown(KeyCode.X))
{
audioManage.PlayAudio(1);
frontLeftWheelCollider.motorTorque = -20000000.0f;
frontRightWheelCollider.motorTorque = -20000000.0f;
}
if (Input.GetKeyDown(KeyCode.W))
audioManage.PlayAudio(3);
if (Input.GetKeyDown(KeyCode.S))
{
audioManage.PlayAudio(2);
effect1.SetActive(false);
effect2.SetActive(false);
}
currentbreakForce = isBreaking ? breakForce : 0f;
if (isBreaking)
{
ApplyBreaking();
}
}
private void ApplyBreaking()
{
frontRightWheelCollider.brakeTorque = currentbreakForce;
frontLeftWheelCollider.brakeTorque = currentbreakForce;
rearRightWheelCollider.brakeTorque = currentbreakForce;
rearRightWheelCollider.brakeTorque = currentbreakForce;
}
private void HandleSteering()
{
currentSteerAngle = maxSteerAngle * HorizontalInput;
frontLeftWheelCollider.steerAngle = currentSteerAngle;
frontRightWheelCollider.steerAngle = currentSteerAngle;
}
private void UpdateWheels()
{
UpdateSinglewheel(frontLeftWheelCollider, frontLeftWheelTransform);
UpdateSinglewheel(frontRightWheelCollider, frontRightWheelTransform);
UpdateSinglewheel(rearRightWheelCollider, rearRightWheelTransform);
UpdateSinglewheel(rearLeftWheelCollider, rearLeftWheelTransform );
}
private void UpdateSinglewheel(WheelCollider wheelCollider, Transform wheelTransform)
{
Vector3 pos;
Quaternion rot;
wheelCollider.GetWorldPose(out pos, out rot);
wheelTransform.rotation = rot;
wheelTransform.position = pos;
}
}
- タイムコードとクロノコード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class GetTime1 : MonoBehaviour
{
public Text TxtCurrentTime;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
DateTime NowTime = DateTime.Now.ToLocalTime();
TxtCurrentTime.text = NowTime.ToString("时间:yyyy-MM-dd HH:mm:ss");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class time : MonoBehaviour
{
private int hour;
private int minute;
private int second;
private int millisecond;
//已经花费的时间
float timeSpeed = 0.0f;
//显示时间区域的文本
Text text_timeSpeed;
// Start is called before the first frame update
void Start()
{
text_timeSpeed = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
timeSpeed += Time.deltaTime;
hour = (int)timeSpeed / 3600;
minute = ((int)timeSpeed - hour * 3600) / 60;
second = (int)timeSpeed - hour * 3600 - minute * 60;
//text_timeSpeed.text = string.Format("{0:D2}:{1:D2}:{2:D2}", hour, minute, second);
millisecond = (int)((timeSpeed - (int)timeSpeed) * 1000);
text_timeSpeed.text = string.Format("计时:{0:D2}:{1:D2}:{2:D2}.{3:D3}", hour, minute, second, millisecond);
}
}
被写体 2 シーン
- 衝突検知タッチ白線コード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class delay : MonoBehaviour
{
[SerializeField] public GameObject text;
private void OnCollisionEnter(Collision collision)
{
//Time.timeScale = 0;
text.SetActive(true);
}
}
- 模擬試験前に白線コード、終了後に白線コードを入力
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tanchuan : MonoBehaviour
{
int i = 0;
public GameObject text;
private void OnCollisionEnter(Collision collision)
{
if (i==0)
{
i = 1;
Time.timeScale = 0;
text.SetActive(true);
}
}
}
要約する
3つのシーンに同じコードスクリプトがあるので、それをそのままコピペするだけです.
これはチームワークです!!
https://download.csdn.net/download/weixin_44954896/85583972?spm=1001.2014.3001.5503全素材のソースコードを含む
プロジェクトファイルをアップロードしました。
開いて実行します。