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

57 lines
1.3 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[Header("Keybinds")]
public KeyCode forwardKey = KeyCode.W;
public KeyCode backwardKey = KeyCode.S;
public KeyCode leftKey = KeyCode.A;
public KeyCode rightKey = KeyCode.D;
[Header("Speed")]
public float speed = 5;
[Header("Player Object")]
public Vector3 moveDirection;
public Rigidbody rb;
[Header("Audio")]
public AudioSource metalPipe;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
}
void Update()
{
moveDirection = PlayerInput();
rb.velocity = moveDirection.normalized * speed;
}
Vector3 PlayerInput()
{
float forward = Convert.ToSingle(Input.GetKey(forwardKey));
float backward = Convert.ToSingle(Input.GetKey(backwardKey));
float right = Convert.ToSingle(Input.GetKey(rightKey));
float left = Convert.ToSingle(Input.GetKey(leftKey));
return new Vector3(right - left, 0, forward - backward);
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Enemy")
{
Debug.Log("OWIE");
metalPipe.Play();
}
}
}