播放器停止移动字符方向重置[Unity 2D]

问题描述 投票:0回答:2

我的角色是一辆汽车,我尝试将其旋转到移动的方向,到目前为止,我成功做到了这一点,但是一旦我停止移动,角色就会翻转回开始时的方向。

而且我如何使从侧面到对面的转弯处变得平滑?

到目前为止是我的代码:

    [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;
 }
unity3d rotation direction
2个回答
0
投票

我不确定这一点,但也许每一帧都被调用的最后一行“ myRigidbody.rotation = angle”是使您的汽车重置其旋转方向的原因。也许将其更改为“ myRigidbody.rotation * =角度”或“ myRigidbody.rotation + =角度”。


0
投票

看起来可能是因为释放控件时将重置Horizo​​ntalcontrolThrow和VerticalcontrolThrow。如果将其重置为原始方向,则说明在移动之前,这两个值将为默认值。然后,您移动并影响旋转。但是,当您释放控件时,这些值又回到了起始值,旋转也是如此。

© www.soinside.com 2019 - 2024. All rights reserved.