Проверка 3D-позиции для сцен внутри группыC#

Место общения программистов C#
Anonymous
Проверка 3D-позиции для сцен внутри группы

Сообщение Anonymous »

У меня есть персонаж игрока, который постоянно движется вперед и может двигаться по 3 «полосам» по оси X, когда игрок переключает полосу движения, аниматор вовремя переключает полосу, однако я бы хотел, чтобы он вернулся на исходную полосу если он сталкивается со сценой в группе «Препятствие» во время анимации. Я решил справиться с этим, проверив два вектора на наличие препятствий прямо перед анимацией. Если препятствий нет, sswitch будет работать как обычно, но если есть препятствие, я хочу найти первую точку на оси X, где находится игрок. и столкновение препятствия, создайте анимацию до этой точки, а затем анимацию до исходного положения игрока по оси X. Однако мне сложно реализовать эту логику из-за отсутствия знаний в Godot. Как я могу проверить сцену в определенной позиции?
using Godot;
using Godot.Collections;

public partial class test_character : CharacterBody3D
{

[Export]
public float currSpeed = 4.0f;
[Export]
public float maxSpeed = 16.0f;
[Export]
public float acceleration = 3.0f;
public float accelerationTime = 0f;
[Export]
public Curve accelerationCurve;
[Export]
public float moveDistance = 3.0f;
private float playerPos = 0;
private bool isCollidingWithObstacle = false;
public float switchDuration = 0.1f;

public override void _PhysicsProcess(double delta)
{
Vector3 velocity = Velocity;

float inputDir = 0f;

if (Input.IsActionJustPressed("move_right"))
{
inputDir = -1;
}

if (Input.IsActionJustPressed("move_left"))
{
inputDir = 1;
}

if (inputDir != 0f)
{

//if player presses either left or right
if (!(playerPos == -1 && inputDir == -1) || !(playerPos == 1 && inputDir == 1))
{
//checks if the player can move to the left or the right

if (playerPos == 0)
{
switchLane(1, inputDir);
}

if ((playerPos == -1 && inputDir == 1) || (playerPos == 1 && inputDir == -1))
{
switchLane(0, inputDir);
}

}
}

Vector3 direction = (Transform.Basis * new Vector3(0, 0, 1)).Normalized();

accelerationTime += (float)delta;
float curveValue = accelerationCurve.Sample(accelerationTime);
currSpeed = Mathf.MoveToward(currSpeed, maxSpeed, acceleration * curveValue * (float)delta);

velocity.Z = direction.Z * currSpeed;

Velocity = velocity;
MoveAndSlide();

}

public void switchLane(int i, float inputDir)
{

/*
How i want the function to work
//Vector3 laneVector = new Vector3(0, GlobalPosition.Y, GlobalPosition.Z);
//Vector3 laneVectorFront = new Vector3(GlobalPosition.X - inputDir * moveDistance, GlobalPosition.Y, GlobalPosition.Z + (currSpeed * switchDuration));

Vector3 laneVector = new Vector3(GlobalPosition.X + inputDir * moveDistance, GlobalPosition.Y, GlobalPosition.Z);
//laneVector - same Position as the player's current position,
//except the X axis corresponds to the lane the player is trying to switch to

Vector3 laneVectorFront = new Vector3(GlobalPosition.X + inputDir * moveDistance, GlobalPosition.Y, GlobalPosition.Z + (currSpeed * switchDuration));
//laneVectorFront - same as laneVector except Z axis is bigger by (currSpeed * switchDuration)
//which should be the vector the player should be on after the tween is succesfully finished

if (checkForObstacle(laneVector) || checkForObstacle(laneVectorFront))
{
GD.Print("Obstacle detected, cant switch");
return;
}
*/

//Currently the function creates a tween that switches the x axis in switchDuration amount of time
Tween tween = GetTree().CreateTween();
tween.SetParallel(true);
tween.SetProcessMode(Tween.TweenProcessMode.Physics);
playerState = _playerState.InputDisabled;
tween.Connect("finished", new Callable(this, "switchDone"));

if (i == 0)
{

tween.TweenProperty(this, "position:x", 0, switchDuration);
playerPos = 0;

}

if (i == 1)
{

tween.TweenProperty(this, "position:x", moveDistance * inputDir, switchDuration);
playerPos = inputDir;

}

}

public void switchDone()
{
playerState = _playerState.Normal;
GD.Print("lane switch finished");
}

}



Подробнее здесь: https://stackoverflow.com/questions/786 ... in-a-group

Вернуться в «C#»