戈多在刷出物体时奇怪的移动



我正在尝试执行一个汽车刷出系统,但我的问题是,当汽车刷出时,它只是以极高的速度飞过关卡。这是生成脚本:

private void Spawn()
{
//Get object and check for errors
var obj = _objectPool.GetObject();
if(obj == null)
return;
//Change location and enable object 
GD.Randomize(); //Randomize seed
var index = (int)GD.RandRange(0, 4);
obj.Position = new Vector2(_pos[index], _posY.GlobalPosition.y);
obj.SetProcess(true);
GD.Print("Spawn" + obj.GlobalPosition);
}// I also have a timer that repeats this function 

对象池脚本:

private void Instantiate(){
_objects = new Node2D[_count];
for(int i=0; i<_count; i++){
//Instanciate object
Node2D node =(Node2D)_scene.Instance();
//Disable it
node.SetProcess(false);
//Add to scene
AddChild(node);
//Add to object list
_objects[i] = node;
}
}
public Node2D GetObject(){
//Return a diactivated object
foreach (var obj in _objects){
if(!obj.IsProcessing())
return obj;
}
return null;
}

最后这是汽车脚本:

public override void _Ready()
{
GD.Randomize();
goingUp = false;
_speed = 10;//(int)GD.RandRange(_minSpeed, _maxSpeed);   
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(float delta)
{
Move(delta);
}   
private void Move(float delta)
{
var motion = Vector2.Zero;
motion = goingUp ? Vector2.Up : Vector2.Down;
//Apply movement
MoveAndSlide(motion);
}

当我不使用刷出器,只是手动添加一辆车到场景中,一切都很好。

当你在场景中添加一个对象时,它会触发物理。很可能是"它以极高的速度飞过关卡"。这是一次碰撞的结果。因此,在将物体添加到场景之前,要给它指定位置。这也意味着你需要告诉你的对象池的位置。

最新更新