所以我是一个编程的初学者,我需要帮助如何使我的精灵速度增加(添加一个冲刺功能)
我试过了,但我失败了,如果有人能解释我怎么做,我会非常感激。
我想要做的是,如果我按下我的Xbox控制器上的右触发器,同时向右移动我的左手拇指杆,将速度从4改为6。
这是我试过的东西,我意识到这不会起作用,因为当我按下他的扳机时,它只会让精灵移动。
if (pad1.Triggers.Right < 0.0f)
position += new Vector2(-6, 0);
if (pad1.Triggers.Right >= 0.4f)
position += new Vector2(6, 0);
我别无选择了,请帮帮忙。
发布于 2014-04-08 03:19:20
这从根本上说是一个物理问题。向该位置添加一个常量只会使精灵移动得更多。你可能正在寻找一种在短时间内提高速度的方法,这就是所谓的加速度。
const float AccelerationForce = 0.ff;
const float MaxAccelerationForce = 0.8f;;
static readonly Vector2 MaxAcceleration
= new Vector2(MaxAccelerationForce, MaxAccelerationForce);
static readonly Vector2 Acceleration = new Vector2(AccelerationForce, 0);
static readonly Vector2 Deceleration = new Vector2(-AccelerationForce, 0);
Vector _currentAcceleration = new Vector2(0, 0);
Vector _currentVelocity = new Vector2(0, 0);
Vector2 Clamp(Vector2 value, Vector2 min, Vector2 max)
{
var x = MathHelper.Clamp(value.X, min.X, max.X);
var y = MathHelper.Clamp(value.Y, min.Y, max.Y);
return new Vector2(x, y);
}
bool RequiresAcceleration(float currentForce)
{
return currentForce >= AccelerationForce;
}
var currentForce = GetCurrentForce();
// Calculate whether we need to accelerate or decelerated
var acceleration = RequiresAcceleration(currentForce)
? Acceleration
: Deceleration;
var newAcceleration = _currentAcceleration + acceleration;
// acceleration is bounded MaxAcceleration > _currentAcceleration > 0
_currentAcceleration = Clamp(newAcceleration, Vector2.Zero, MaxAcceleration);
_currentVelocity += _currentAcceleration;
// Code to move the player
if (pad1.Triggers.Right < 0.0f)
position += _currentVelocity);
if (pad1.Triggers.Right >= 0.4f)
position -= _currentVelocity;
发布于 2014-04-08 02:09:54
我相信这就是你想要实现的。另外,编辑帖子是相当容易的,请编辑帖子,而不是评论与更新。
if (pad1.Triggers.Right < 0)
// This checks if left thumbstick is less than 0.
// If it is less than zero it sets position.X += -6.
// If it is not less then it sets position.X += -4.
position.X -= (pad1.ThumbSticks.Left.X < 0) ? 6 : 4;
else if (pad1.Triggers.Right >= 0.4f) // shouldn't this be > 0.0f or > 0 ?
// This is the same as above just in the right direction.
position.X += (pad1.ThumbSticks.Left.X > 0) ? 6 : 4;
https://stackoverflow.com/questions/22918596
复制相似问题