BloodAndGore/Assets/Scripts/Player.cs

101 lines
3.1 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
namespace Saltosion.OneWeapon {
public class Player : MonoBehaviour {
public Rigidbody2D Body;
public Transform Hand;
public float MoveSpeed = 50f;
public SpriteRenderer BodySprite;
public Animator BodyAnim;
public SpriteRenderer HeadSprite;
public Animator HeadAnim;
private Gun Gun;
private Vector2 GunLocation = new Vector2(0, -0.6f);
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;
Vector3 Rot = Hand.localEulerAngles;
Rot.z = Mathf.Atan2(LookDirection.y, LookDirection.x) * Mathf.Rad2Deg + 90;
Hand.localEulerAngles = Rot;
float Rotation = Rot.z;
if (Rotation >= 45 && Rotation < 135) {
BodyAnim.Play("HorizontalBody");
BodySprite.flipX = false;
HeadAnim.Play("HeadRight");
HeadSprite.flipX = false;
} else if (Rotation >= 225 || Rotation < -45) {
BodyAnim.Play("HorizontalBody");
BodySprite.flipX = true;
HeadAnim.Play("HeadRight");
HeadSprite.flipX = true;
} else {
if (Rotation >= 135 && Rotation < 225) {
HeadAnim.Play("HeadUp");
} else {
HeadAnim.Play("HeadDown");
}
BodyAnim.Play("VerticalBody");
BodySprite.flipX = false;
HeadSprite.flipX = false;
}
if (Gun != null) {
if (Rotation >= 180 || Rotation <= -45) {
Gun.FlipGun(true);
} else if (Rotation < 180 || Rotation > 45) {
Gun.FlipGun(false);
}
}
if (Direction.magnitude > 0.05) {
BodyAnim.SetFloat("Speed", 1);
HeadAnim.SetFloat("Speed", 1);
} else {
BodyAnim.SetFloat("Speed", 0);
HeadAnim.SetFloat("Speed", 0);
}
bool Shoot = Input.GetButtonDown("Shoot");
if (Shoot && Gun != null) {
Gun.Shoot(LookDirection, Rotation);
}
}
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;
}
}
}