summaryrefslogtreecommitdiff
path: root/Assets/Scripts/PlayerMovement.cs
blob: 90252b3aee708e2e214c3039eda00b6278afcde5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using UnityEngine;

[RequireComponent(typeof(PlayerControl))]
[RequireComponent(typeof(Rigidbody))]
public class PlayerMovement : MonoBehaviour
{
    public float movementSpeed;
    public float movementSpeedOnGrabbing;
    
    private PlayerControl _playerControl;
    private Rigidbody _rigidbody;
    
    private void Awake()
    {
        _playerControl = GetComponent<PlayerControl>();
        _rigidbody = GetComponent<Rigidbody>();
    }

    private void FixedUpdate()
    {
        FixedUpdateMovement();
    }

    private void FixedUpdateMovement()
    {
        var velocity = _rigidbody.linearVelocity;
        var speed = _playerControl.grabbing ?  movementSpeedOnGrabbing : movementSpeed;
        var direction = new Vector3(
            _playerControl.movementHorizontal,
            0,
            _playerControl.movementVertical)
            .normalized;
        var playerVelocity = speed * direction + velocity.y * Vector3.up;
        _rigidbody.linearVelocity = playerVelocity;
    }
}