UnityФорумПрограммирование

Игрок отскакивает от других объектов на сцене

#0
10:30, 20 июня 2024

При прикосновении моего персонажа с какой-либо стеной или объектом, он начинает отталкиваться.

Вот код:
(Код передвижения)

public interface IInputSystems
{
    public void Move(float speed, GameObject _player);
}

public abstract class InputSystems : MonoBehaviour, IInputSystems
{
    public abstract void Move(float speed, GameObject _player);
}


public class KeyboardDefaultInputSystem : InputSystems
{
    float Vertical;
    float Horizontal;
    private float speed;
    private GameObject _player;
    private Rigidbody _rigidbody;

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.Escape))
        {
            EventBus.menu?.Invoke();

        }        
    }

    public override void Move(float speed, GameObject _player)
    {
        this.speed = speed;
        this._player = _player;
        this._rigidbody = _player.GetComponent<Rigidbody>();
        Vertical = Input.GetAxis("Vertical");
        Horizontal = Input.GetAxis("Horizontal");
        if (Vertical != 0)
        {
            Forward();

        }
        if (Horizontal != 0)
        {
            Right();

        }
        Debug.Log(_rigidbody.velocity.magnitude);

    }

    private void Forward()
    {

        _player.transform.position += _player.transform.forward * Vertical * speed * Time.deltaTime;

        //_rigidbody.AddForce(transform.forward * Vertical * speed);
        

        //this.transform.position += this.transform.forward * speed * Time.deltaTime;
    }



    private void Right()
    {

        //_rigidbody.AddForce(transform.forward * Vertical * speed);
        
        _player.transform.position += _player.transform.right * Horizontal * speed * Time.deltaTime;
    }
}

Код персонажа:

    public class PlayerController : Player
    {
        private IInputSystems _inputSystem;
        private ICameraSystem _cameraSystem;
        private void Awake()
        {
            Cursor.lockState = CursorLockMode.Locked;
            _inputSystem = this.transform.AddComponent<KeyboardDefaultInputSystem>();
            _cameraSystem = this.transform.AddComponent<DefaultCameraSystemWithBobbing>();
        }

        void Update()
        {

            _cameraSystem.Camera_Rotate(sensitivity, smoothing, _camera_parent, _player);

        }

        private void FixedUpdate()
        {
            _inputSystem.Move(speed, this.gameObject);

        }
    }
}

Знаю что это происходит из-за того что один коллайдер входит в другой из-за обновления позиции, а не добавления силы через AddForce - FixedUpdate делает картинку дерганой, rigidbody.AddForce крайне неудобная система

Есть-ли тут еще какое-нибудь решение?

UnityФорумПрограммирование

Тема в архиве.

Тема закрыта.