[英]Character Movement, Acceleration C# Unity
Hi everyone Newbie here.大家好,这里的新手。
Top-down Zelda style game.自上而下的塞尔达风格游戏。
I'm trying to figure out how to make my player build speed to max speed then reduce speed to stoping.我试图弄清楚如何让我的播放器建立速度到最大速度然后降低速度到停止。
I already have movement with GetRawAxis but my char moves at max speed the moment I press move with this method.我已经使用 GetRawAxis 进行了移动,但是当我使用此方法按下移动时,我的字符以最大速度移动。
private void PlayerMovement()
{
var playerMovement = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized * Time.deltaTime * moveSpeed;
transform.position = new Vector3(
transform.position.x + playerMovement.x,
transform.position.y + playerMovement.y,
transform.position.z);
}
Here is a scenario where you can move your object in the x axis, gradually increasing the speed.这是一个场景,您可以在 x 轴上移动 object,逐渐提高速度。 You can do the same with the slowing down.
你可以对减速做同样的事情。 Gradually decrease the speed by a value.
逐渐将速度降低一个值。
float acceleration = 0.6;
float maxSpeed = 30;
float speed = 0;
void Update(){
if(speed < maxSpeed){
speed += acceleration * Time.deltaTime;
}
transform.position.x = transform.position.x + speed*Time.deltaTime;
}
You could try Vector3.SmoothDamp
.你可以试试
Vector3.SmoothDamp
。
This uses a Vector storing the current "speed" and dumps it slowly.这使用存储当前“速度”的向量并缓慢转储。
Example can be found here: https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html示例可以在这里找到: https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.