Скрипт перемещения машин

This commit is contained in:
LikhenkoVG
2024-12-17 21:46:56 +03:00
parent 8358a1dcd6
commit 937a6340ec
18 changed files with 2833 additions and 48 deletions

View File

@ -0,0 +1,26 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Скрипт для анимации машины
/// </summary>
public class CarControllerScript : MonoBehaviour
{
/// <summary>
/// to do
/// </summary>
[SerializeField]
private List<GameObject> _Wheels = new List<GameObject>();
[SerializeField]
private int _Speed;
private void FixedUpdate()
{
foreach (var wheel in _Wheels)
{
wheel.transform.Rotate(Vector3.right * _Speed);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c4f2135cee0d6334480a3a7a63ffb29a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using UnityEngine;
public class CarManagerScript : MonoBehaviour
{
[SerializeField]
private List<GameObject> _CarPrefs;
[SerializeField]
private GameObject[] _Points = new GameObject[2];
[SerializeField]
bool _LeftToRight;
private void Start()
{
_LeftToRight = GetRandomObject<bool>(new List<bool> { true, false });
StartMove();
}
[SerializeField]
private int _Speed;
private void FixedUpdate()
{
if (_Car != null)
{
if (_LeftToRight)
{
_Car.transform.Translate(Vector3.forward * Time.deltaTime * _Speed);
}
else if (!_LeftToRight)
{
_Car.transform.Translate(Vector3.forward * Time.deltaTime * _Speed);
}
}
}
private GameObject _Car;
private void StartMove()
{
Vector3 pos;
Quaternion rot;
if (_LeftToRight)
{
pos = _Points[0].transform.position;
rot = Quaternion.Euler(0, 90, 0);
_Car = Instantiate(GetRandomObject<GameObject>(_CarPrefs), pos, rot);
}
else if (!_LeftToRight)
{
pos = _Points[1].transform.position;
rot = Quaternion.Euler(0, -90, 0);
_Car = Instantiate(GetRandomObject<GameObject>(_CarPrefs), pos, rot);
}
//_Car.transform.SetParent(gameObject.transform, _Points[1].transform);
}
public void DeleteCar()
{
Destroy( _Car );
StartMove();
}
T GetRandomObject<T>(List<T> list)
{
// проверка на пустоту списка
if (list == null || list.Count == 0)
{
return default;
}
// Генерация случайного индекса
int randomIndex = UnityEngine.Random.Range(0, list.Count);
return list[randomIndex];
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0496cf2f238f72d4188cc7298b91a038
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: