Mini-Games-Game/Assets/Scripts/PlayerMovement.cs

45 lines
1.2 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("Speed")]
public float acceleration;
public float maxSpeed;
[Header("Movement")]
public float horizontalInput;
public float verticalInput;
public Vector3 moveDirection;
public Rigidbody rb;
public float drag;
public Transform orientation;
public Vector3 speed;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
rb.drag = drag;
speed = new Vector3(0f, 0f, 0f);
}
void Update()
{
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
moveDirection = orientation.forward * verticalInput + orientation.up * 0 + orientation.right * horizontalInput;
moveDirection = moveDirection.normalized * acceleration;
rb.AddForce(moveDirection);
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = rb.velocity.normalized * maxSpeed;
}
// rb.velocity.Set(Vector3.ClampMagnitude(moveDirection, maxSpeed));
speed.Set(rb.velocity.x, rb.velocity.magnitude, rb.velocity.y);
}
}