我正在做一个2D的多人游戏,我不知道如何解决物体碰撞后很长时间的旋转。
Video of problem
对象的组件:
移动和旋转对象代码:
using UnityEngine;
using Photon.Pun;
[RequireComponent(typeof(Rigidbody2D), typeof(CapsuleCollider2D))]
public class PlayerMover : MonoBehaviour
{
[SerializeField] private float _speepMoving, _speedRotation;
private Rigidbody2D _rigidbody;
private Vector2 _direction;
private PhotonView _view;
[HideInInspector] public FixedJoystick joystick;
private void Start()
{
_view = GetComponent<PhotonView>();
_rigidbody = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
if (_view.IsMine)
{
_rigidbody.velocity = new Vector2(joystick.Horizontal * _speepMoving, joystick.Vertical * _speepMoving);
RotatePlayer();
}
}
private void RotatePlayer()
{
_direction = new Vector2(joystick.Horizontal, joystick.Vertical);
if (_direction != Vector2.zero)
{
Quaternion toRotation = Quaternion.LookRotation(Vector3.forward, _direction);
_rigidbody.MoveRotation(Quaternion.Lerp(transform.rotation, toRotation, _speedRotation * Time.deltaTime));
}
}
}
1条答案
按热度按时间5cg8jx4n1#
如果你不想让你的物体旋转,你必须在你的刚体中使用冻结旋转属性。现在,刚体撞到自己,他们开始旋转,没有任何东西阻止他们的旋转。你冻结旋转z,它应该停止旋转。