BloodAndGore/Assets/Scripts/Player.cs

55 lines
1.5 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Saltosion.OneWeapon {
public class Player : MonoBehaviour {
public Rigidbody2D Body;
public Transform Hand;
public float MoveSpeed = 50f;
private Gun Gun;
private Vector2 GunLocation = new Vector2(0.1f, -0.7f);
private Vector3 GunRotation = new Vector3(0, 0, -90);
void Start() {
}
void Update() {
float X = Input.GetAxis("Horizontal");
float Y = Input.GetAxis("Vertical");
Vector2 Direction = new Vector2(X, Y).normalized;
Body.velocity = Direction * MoveSpeed;
Vector3 MousePos = Input.mousePosition;
Vector3 PointedPos = Camera.main.ScreenToWorldPoint(MousePos);
Vector2 LookDirection = (PointedPos - Hand.position).normalized;
var Rot = Hand.localEulerAngles;
Rot.z = Mathf.Atan2(LookDirection.y, LookDirection.x) * Mathf.Rad2Deg + 90;
Hand.localEulerAngles = Rot;
bool Shoot = Input.GetButtonDown("Shoot");
if (Shoot && Gun != null) {
Gun.Shoot(Gun.transform.position, LookDirection, Rot.z);
}
}
public void SetGun(Gun gun) {
if (Gun != null) {
Destroy(Gun.gameObject);
}
Gun = gun;
Gun.transform.parent = Hand;
Gun.transform.localPosition = GunLocation;
Gun.transform.localEulerAngles = GunRotation;
}
}
}