我的角色是一辆汽车,我尝试将其旋转到移动的方向,到目前为止,我成功做到了这一点,但是一旦我停止移动,角色就会翻转回开始时的方向。
而且我如何使从侧面到对面的转弯处变得平滑?
到目前为止是我的代码:
[SerializeField] float driveSpeed = 5f;
//state
Rigidbody2D myRigidbody;
// Start is called before the first frame update
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
Move();
}
private void Move()
{
//Control of velocity of the car
float HorizontalcontrolThrow = CrossPlatformInputManager.GetAxis("Horizontal"); // Value between -1 to 1
float VerticalcontrolThrow = CrossPlatformInputManager.GetAxis("Vertical"); // Value between -1 to 1
Vector2 playerVelocity = new Vector2(HorizontalcontrolThrow * driveSpeed, VerticalcontrolThrow * driveSpeed);
myRigidbody.velocity =playerVelocity;
**//Direction of the car**
Vector2 direction = new Vector2(HorizontalcontrolThrow, VerticalcontrolThrow);
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
myRigidbody.rotation = angle;
}
我不确定这一点,但也许每一帧都被调用的最后一行“ myRigidbody.rotation = angle”是使您的汽车重置其旋转方向的原因。也许将其更改为“ myRigidbody.rotation * =角度”或“ myRigidbody.rotation + =角度”。
看起来可能是因为释放控件时将重置HorizontalcontrolThrow和VerticalcontrolThrow。如果将其重置为原始方向,则说明在移动之前,这两个值将为默认值。然后,您移动并影响旋转。但是,当您释放控件时,这些值又回到了起始值,旋转也是如此。