Untitled

 avatar
unknown
csharp
2 years ago
1.3 kB
6
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

[RequireComponent(typeof(Rigidbody))]
public class PlayerMovement : MonoBehaviour
{
    [SerializeField] float movementSpeed = 10f;
    [SerializeField] float movementSpeedMultiplier = 10f;
    [SerializeField] Transform orientation;

    PlayerInputActions controls;
    Rigidbody rb;

    Vector2 movementInput;
    Vector3 movementDir;

    void Awake()
    {
        controls = new PlayerInputActions();
    }

    void OnEnable()
    {
        controls.Enable();
    }

    void OnDisable()
    {
        controls.Disable();
    }

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.freezeRotation = true;
    }

    void Update()
    {
        GetInput();
    }

    void FixedUpdate()
    {
        PlayerMove();
    }

    void GetInput()
    {
        movementInput = controls.Player.Movement.ReadValue<Vector2>();
    }

    void PlayerMove()
    {
        movementDir = orientation.forward * movementInput.y + orientation.right * movementInput.x;

        rb.AddForce(movementDir.normalized * movementSpeed * movementSpeedMultiplier, ForceMode.Force);
    }
}
Editor is loading...