提问人:BloodShot 提问时间:11/16/2023 最后编辑:Mark RotteveelBloodShot 更新时间:11/18/2023 访问量:120
跳入 Godot 感觉很糟糕,但代码似乎还可以 [关闭]
jump in godot feels so bad but the code seems to be ok [closed]
问:
跳跃是如此突然和紧张。 我想这可能是因为我将游戏的分辨率设置为 1980x1080,但谁知道呢。 当我想改变角色时,脚本还会切换动画。我不认为这是最好的方法,但它有效,但正因为如此,我不得不禁用插值。我不知道为什么。 代码如下:
using Godot;
using System;
public partial class PlayerController : CharacterBody2D
{
#region PROCESS
private Sprite2D _sprite;
private AnimationPlayer _AnimationPlayer;
public override void _Ready()
{
var slowedTimer = GetNode<Timer>("Timer");
_sprite = GetNode<Sprite2D>("Sprite2D");
//_sprite.TopLevel = true;
_AnimationPlayer = GetNode<AnimationPlayer>("AnimationPlayer");
slowedTimer.Start();
}
public override void _Process(double delta)
{
GhatherInput();
//_sprite.GlobalPosition = _sprite.GlobalPosition.Lerp(GlobalPosition,(float)Engine.GetPhysicsInterpolationFraction());
}
public override void _PhysicsProcess(double delta)
{
CalculateVelocity((float)delta);
MoveAndSlide();
}
#endregion
#region Input
private Vector2 _input;
private bool _DidJump = false;
private int charachter = 1;
private void GhatherInput()
{
_input.X = 0;
_input.Y = 0;
if(Input.IsActionPressed("up")) _input.Y -= 1;
if(Input.IsActionPressed("right")) _input.X += 1;
if(Input.IsActionPressed("down")) _input.Y += 1;
if(Input.IsActionPressed("left")) _input.X -= 1;
if(Input.IsActionJustPressed("jump")) _DidJump = true;
if(Input.IsActionJustPressed("ch1"))
{
charachter = 1;
_speed = 300f;
jumpforce = -6000f;
_Slowspeed = 100f;
}
if(Input.IsActionJustPressed("ch2"))
{
charachter = 2;
_speed = 200f;
jumpforce = -6000f;
_Slowspeed = 50f;
}
if(Input.IsActionJustPressed("ch3")) charachter = 3;
}
#endregion
#region Gravity
[Export] private float _gravity = 20000f;
[Export] private float _terminalvelocity = 600f;
private void ApplyGravity(ref Vector2 vec,float delta){
if(IsOnFloor()) return;
lastvelocityY=vec.Y;
newvelcityY = (vec.Y + _gravity) * delta;
newvelcityY = Mathf.Clamp(newvelcityY, -Mathf.Inf , _terminalvelocity);
vec.Y = (lastvelocityY + newvelcityY) * .5f;
}
#endregion
#region Velocity
private Vector2 _velocity;
private float lastvelocityY,newvelcityY;
[Export] private float _speed = 300f;
[Export] private float _Slowspeed = 100f;
[Export] private float _acseel = 15f;
[Export] private float _DEacseel = 10f;
private float _targetVelocityX,_SlowtargetVelocityX;
private bool slowed = false;
private void CalculateVelocity(float delta)
{
_velocity = Velocity;
CalculateVelocityY(ref _velocity,delta);
CalculateVelocityX(ref _velocity);
Velocity = _velocity;
}
private void CalculateVelocityY(ref Vector2 vec,float delta)
{
ApplyGravity(ref vec,delta);
HandleJump(ref vec,delta);
}
private void CalculateVelocityX (ref Vector2 vec)
{
_targetVelocityX = _input.X * _speed;
_SlowtargetVelocityX = _input.X * _Slowspeed;
if (slowed)
{
vec.X = Mathf.MoveToward(vec.X,_SlowtargetVelocityX,Mathf.Sign(_input.X) == Mathf.Sign(_velocity.X) ? _acseel:_DEacseel);
}
else
{
vec.X = Mathf.MoveToward(vec.X,_targetVelocityX,Mathf.Sign(_input.X) == Mathf.Sign(_velocity.X) ? _acseel:_DEacseel);
}
HandleAnimation();
}
#endregion
#region Slow_calculator
public void OnTimerTimeOut()
{
var slowedTimer2 = GetNode<Timer>("Timer2");
slowed = true;
slowedTimer2.Start();
}
public void ontimertimeouttwo()
{
slowed = false;
}
#endregion
#region jump
[Export] private float jumpforce = -6000f;
private void HandleJump(ref Vector2 vec,float delta)
{
if (!IsOnFloor() || !_DidJump) return;
_DidJump = false;
vec.Y = jumpforce;
}
#endregion
#region Animation
private void HandleAnimation()
{
HandleFlip();
Handlecharechter();
}
private void HandleFlip()
{
if (_velocity.X == 0) return;
if (_velocity.X < 0) _sprite.FlipH = true;
if (_velocity.X > 0) _sprite.FlipH = false;
}
private void Handlecharechter()
{
if(charachter == 1)
{
_AnimationPlayer.Play("one_idle");
}
if(charachter == 2)
{
_AnimationPlayer.Play("two_idle");
}
}
#endregion
}
我想要一个平稳的跳跃,感觉不错。此外,此代码还包括玩家放慢速度的部分,因为我希望水滴在我的游戏中移动。
答: 暂无答案
评论