在mapmanager脚本中添加代码
import { _decorator, Component, Node } from 'cc';
import { Map123 } from './Map123';
const { ccclass, property } = _decorator;
@ccclass('MapManager')
export class MapManager extends Component {
public currentPath :Node[] = []; //当前地图的路径。路径可能是3条,或者多条。
public resetMap (){
//const currentMap = this.node.children[0].getComponent(Map123) ;这行报错
const currentMap = this.node.children[0].getComponent(Map123) as Map123;
this.currentPath = currentMap.path;
}
}在gamectrl脚本中添加代码 调用mapmanager的resetmap

上面的操作实际上就是拿到3个点。 3个路径的开始点。

CarManager 代码如下
import { _decorator, Component, Node } from 'cc';
import { Car } from './Car';
const { ccclass, property } = _decorator;
@ccclass('CarManager')
export class CarManager extends Component {
@property({
type: Car
})
mainCar: Car | null = null;
//mainCai :Car = null;
/**
*
* points是地图中3条,路径的3个起始点。
*/
public resetCars(points: Node[]) {
if (points.length <= 0) {
console.log("没有point在map");
return;
}
//初始化小车 放到第1个路径的,起点点。
this._createMainCar(points[0]);
}
public controMoving(isRunnding = true) {
if (isRunnding) {
//这里能不能通过getComm调用。
this.mainCar?.startRunning();
}else{
this.mainCar?.stopRunning();
}
}
private _createMainCar(point: Node) {
this.mainCar?.setEntry(point);
}
}

public _currentRoadPoint :RoadPoint |null= null; private _pointA = new Vec3(); private _pointB = new Vec3();
/**
*
* @param entry 是第1条路径 的起始点。上面有脚本RoadPoint
*/
public setEntry(entry: Node) {
//世界坐标,绝对位置,他在别的模型的位置,可能有偏移量,不准。所以使用世界坐标系
this.node.setWorldPosition(entry.worldPosition);
//这是传来的 1条直接的2个点。创建路径的点。上面有RoadPoint脚本
this._currentRoadPoint = entry.getComponent(RoadPoint) as RoadPoint;
if (!this._currentRoadPoint) {
console.log("这个点没有roadPoint" + entry.name);
}
//A就是我起点(当前的点 小车自身的点)。B就是下一个点。
this._pointA.set(entry.worldPosition);
this._pointB.set(this._currentRoadPoint.nextStation.worldPosition);
//计算车身,是否需要,转动方向
//下一点的z 减去 小车的z 如果是0 如果不是0
// 原理我是知道,但是为什么会有这种效果。我不知道。
const z = this._pointB.z - this._pointA.z;// 50 - 80
console.log("z:"+z);
if (z !== 0) {
if (z < 0) {
console.log("z是负数,没有偏移不用转向")
//没有偏移不用转向 这个函数,经过测试 会自动修正方向
this.node.eulerAngles = new Vec3();
} else {
console.log("z是正数")
//旋转用的是y转。
this.node.eulerAngles = new Vec3(0, 180, 0);
}
} else {
const x = this._pointB.x - this._pointA.x;
console.log("x:"+x);
if (x > 0) {
console.log("x是正数")
this.node.eulerAngles = new Vec3(0, 270, 0);
} else {
console.log("x是负数")
this.node.eulerAngles = new Vec3(0, 90, 0);
}
}
}这个时候小车 已经初始化到第1个点上面了。
这里可以讲一下,小车方向的调整。朝向问题。
站长微信:xiaomao0055
站长QQ:14496453