Unity游戏开发:车辆和工具车管理器脚本
该脚本是一个管理器,用于生成汽车和工具车。其中包含了一些静态变量,用于表示汽车的朝向、方向等信息。通过调用CreateCar方法可以生成一辆汽车,需要传入汽车的编号、出生车道、换道后车道、初始朝向、通过交叉口后的朝向、换道的位置距离交叉口的远近、初始生成位置距离交叉口的横向距离等信息。CreateGongju方法用于生成工具车,需要传入工具车的出生车道、通过交叉口后的朝向、初始生成位置距离交叉口的横向距离等信息。这些信息可以通过读取场景中的Point物体来获取。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MANAGER : MonoBehaviour
{
public static Vector3[] Rotation = new Vector3[5] { Vector3.one, new Vector3(0, 90, 0), new Vector3(0, -90, 0), Vector3.zero, new Vector3(0, 180, 0) }; //这里用来表示汽车的朝向 E W N S
public static Vector3[] Dir = new Vector3[5] { Vector3.one, new Vector3(1, 0, 0), new Vector3(-1, 0, 0), new Vector3(0, 0, 1), new Vector3(0, 0, -1) };
public GameObject CarPfab;
public GameObject CarGongju;
//public enum Dir { E=1, W, S, N }; //车辆的方向
//public static Vector3[] EnterDir = new Vector3[5] { Vector3.zero, new Vector3(1, 0, 0), new Vector3(-1, 0, 0), new Vector3(0, 0, -1), new Vector3(0, 0, 1) };//开始的方位
//public static Vector3[] ExitDir = new Vector3[5] { Vector3.zero, new Vector3(-1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0, 0, -1) };//转弯结束的方位
//public static Vector3[] EnterRotation = new Vector3[5] { Vector3.zero, new Vector3(0,90,0), new Vector3(0,270,0), new Vector3(0,180,0), new Vector3(0,0,0) };//开始的角度
//public enum Pos { A,B,C,D};//行人车的目标
//public int CarNum;//车辆数量
//public Transform N1, N2, N3, N4, S1, S2, S3, S4, W1, W2, W3, W4, E1, E2, E3, E4,center;
private void Start()
{
/*
CreateCar(1,2,2,3,3,4,4); //直行
CreateCar(2,3,3,3,3,7,7); //
CreateCar(21, 3, 3, 3, 3, 15, 15); //
CreateCar(3,4,4,3,1,-35,-15); //
CreateCar(4,1,1,3,2,3,3); //
CreateCar(41, 1, 1, 3, 2, -23, -3); //
CreateCar(42, 1, 1, 3, 2, 16, 16); //
CreateCar(5, 2, 2, 2, 2, 3, 3); //直行
CreateCar(51, 2, 2, 2, 2, -63, -13); //直行
CreateCar(6, 3, 3, 2, 3, 9, 9); //
CreateCar(61, 3, 3, 2, 2, -44, -8); //
CreateCar(7, 4, 4, 2, 1, -63, -23); //
CreateCar(71, 4, 4, 2, 1, 13, 17); //
CreateCar(8, 1, 1, 2, 3, 5, 5); //
CreateCar(81, 1, 1, 2, 3, -25, -5); //
CreateCar(9, 2, 2, 1, 3, 14, 14); //直行
CreateCar(10, 3, 3, 1, 2, -64, -8); //
CreateCar(11, 4, 4, 1, 1, 3, 3); //
CreateCar(12, 1, 1, 1, 3, -24, -9); //
CreateCar(121, 1, 1, 1, 3, 4, 19); //
CreateCar(13, 2, 2, 4, 3, -54, -11); //直行
CreateCar(14, 3, 3, 4, 2, -44, -4); //
CreateCar(15, 4, 4, 4, 1, 4, 4); //
CreateCar(16, 1, 1, 4, 4, 9, 11); //
CreateCar(161, 1, 1, 4, 4, 13, 13); //
*/
//初始化分别表示 1.汽车编号 2.出生车道 3.换道后车道 4.初始朝向 5.通过交叉口后的朝向 6.换道的位置距离交叉口的远近 7.初始生成位置距离交叉口的横向距离
//z轴正
CreateCar(1, 2, 1, 3, 2, 45, 48); //左转 5
CreateCar(2, 1, 2, 3, 3, 51, 60); //直行 4
CreateCar(3, 4, 3, 3, 3, 46, 55); //直行 6
CreateCar(4, 4, 4, 3, 1, 20, 68);
Debug.Log("!");
GameObject c = GameObject.FindGameObjectWithTag("MyCamera");
Debug.Log(c.name);
c.transform.SetParent(CreateCar(5, 1, 2, 3, 2, 50, 79));
c.transform.localPosition = new Vector3(-0.04f, 1.22f, -1.12f);
c.transform.rotation = Quaternion.Euler(new Vector3(-1, 0, 0));
Debug.Log("jj" + c.transform.rotation);
Debug.Log("相机" + c.transform.localPosition);
CreateCar(6, 2, 3, 3, 3, 55, 87); //直行 2
CreateCar(7, 2, 1, 3, 3, 57, 90); //直行 3
CreateCar(8, 4, 4, 3, 3, 24, 97); //直行
//z轴负
CreateCar(9, 1, 1, 4, 1, 13, 45); //左转
CreateCar(10, 2, 2, 4, 4, 17, 61); //直行
CreateCar(11, 3, 3, 4, 4, 23, 85); //直行
CreateCar(12, 4, 4, 4, 2, 31, 68); //右转
CreateCar(13, 1, 2, 4, 1, 34, 120); //左转
CreateCar(14, 2, 3, 4, 4, 42, 85); //直行
CreateCar(15, 3, 1, 4, 4, 38, 88); //直行
CreateCar(16, 4, 4, 4, 4, 20, 95); //直行
//x轴正
CreateCar(17, 1, 1, 1, 1, 3, 42); //直行
CreateCar(18, 2, 2, 1, 4, 36, 61); //右转
//CreateCar(19, 3, 3, 1, 1, 10, 55); //直行
//CreateCar(20, 4, 4, 1, 3, 19, 68); //左
CreateCar(21, 1, 2, 1, 4, 39, 75); //直行
CreateCar(22, 2, 3, 1, 1, 42, 99); //直行
CreateCar(23, 3, 1, 1, 3, 35, 120); //左
CreateCar(24, 4, 4, 1, 4, 24, 115); //右
//x轴负
//CreateCar(25,3,3,2,3,30,62); //左转
CreateCar(26, 1, 2, 2, 2, 42, 53); //直行
CreateCar(27, 4, 4, 2, 2, 24, 71); //直行
CreateCar(28, 2, 1, 2, 3, 39, 45); //右转
CreateCar(29, 2, 2, 2, 2, 5, 51); //直行
CreateCar(30, 3, 3, 2, 4, 7, 77); //左转
CreateCar(31, 1, 1, 2, 3, 3, 57); //右转
//CreateCar(32,4,4,2,2,19,65); //直行
//工具车
createGongju(1, 4, -10);
createGongju(2, 4, 0);
createGongju(4, 4, 20);
createGongju(3, 4, 13);
createGongju(3, 4, 10);
createGongju(2, 4, -21);
createGongju(1, 4, -5);
createGongju(4, 4, 8);
createGongju(1, 4, 33);
createGongju(2, 4, 45);
createGongju(4, 4, 56);
createGongju(3, 4, 29);
createGongju(3, 4, 76);
createGongju(2, 4, 69);
createGongju(1, 4, 52);
createGongju(4, 4, 88);
createGongju(2, 3, -22);
createGongju(1, 3, 5);
createGongju(1, 3, 23);
createGongju(4, 3, -28);
createGongju(3, 3, 11);
createGongju(3, 3, -15);
createGongju(1, 3, -2);
createGongju(2, 3, 9);
createGongju(2, 3, 33);
createGongju(1, 3, 45);
createGongju(1, 3, 56);
createGongju(4, 3, 29);
createGongju(3, 3, 76);
createGongju(3, 3, 69);
createGongju(1, 3, 52);
createGongju(2, 3, 88);
createGongju(4, 1, -25);
createGongju(1, 1, 5);
createGongju(3, 1, 23);
createGongju(4, 1, 2);
createGongju(3, 1, 11);
createGongju(2, 1, -17);
createGongju(1, 1, -2);
createGongju(2, 1, -10);
createGongju(4, 1, 33);
createGongju(1, 1, 45);
createGongju(3, 1, 56);
createGongju(4, 1, 29);
createGongju(3, 1, 76);
createGongju(2, 1, 69);
createGongju(1, 1, 52);
createGongju(2, 1, 88);
createGongju(1, 2, -16);
createGongju(3, 2, 6);
createGongju(3, 2, 23);
createGongju(4, 2, 15);
createGongju(1, 2, 12);
createGongju(2, 2, -20);
createGongju(4, 2, -10);
createGongju(2, 2, 10);
createGongju(1, 2, 33);
createGongju(3, 2, 45);
createGongju(3, 2, 56);
createGongju(4, 2, 29);
createGongju(1, 2, 76);
createGongju(2, 2, 69);
createGongju(4, 2, 52);
createGongju(2, 2, 88);
}
int x = 1;
private void Update()
{
}
public Transform CreateCar(int Num, int BornL, int SwitchL, int EnterD, int ExitD, int x1,int x2)
{ //3 1 1 E 3 N
var Car = Instantiate(CarPfab);
var component = Car.GetComponent<CarCon>();
component.Num = Num;
component.BornL = BornL;
component.SwitchL = SwitchL;
component.EnterD = Rotation[EnterD];
component.ExitD = Rotation[ExitD];
GameObject point = GameObject.FindGameObjectWithTag("Point");
component.TurnStart = point.transform.GetChild(EnterD - 1).GetChild(0).GetChild(SwitchL - 1).position;
Debug.Log(component.TurnStart);
component.TurnEnd = point.transform.GetChild(ExitD - 1).GetChild(1).GetChild(SwitchL - 1).position;
Debug.Log(component.TurnEnd);
component.x1 = x1;
component.EnterDir = Dir[EnterD];
component.ExitDir = Dir[ExitD];
component.D1 = EnterD;
component.D2 = ExitD;
Vector3 temp = point.transform.GetChild(EnterD - 1).GetChild(0).GetChild(BornL - 1).position;
if (EnterD == 1) component.BornP = new Vector3(temp.x - x2, temp.y, temp.z);
else if (EnterD == 2) component.BornP = new Vector3(temp.x + x2, temp.y, temp.z);
else if (EnterD == 3) component.BornP = new Vector3(temp.x, temp.y, temp.z - x2);
else if (EnterD == 4) component.BornP = new Vector3(temp.x, temp.y, temp.z + x2);
return Car.transform;
}
void createGongju(int BornL,int ExitD,int x)
{
var Car = Instantiate(CarGongju);
var component = Car.GetComponent<Gongju>();
GameObject point = GameObject.FindGameObjectWithTag("Point");
component.bornP= point.transform.GetChild(ExitD - 1).GetChild(1).GetChild(BornL - 1).position;
component.p = Dir[ExitD];
component.x = x;
component.retate = Rotation[ExitD];
}
}
原文地址: https://www.cveoy.top/t/topic/nu5J 著作权归作者所有。请勿转载和采集!