139 lines
4.4 KiB
C#
139 lines
4.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class PlayerMovement : MyTypes.MyPawn{
|
|
|
|
public bool player1;
|
|
|
|
[Space]
|
|
[SerializeField] PlayerManager playerManager;
|
|
|
|
Vector2 velocity;
|
|
bool moving;
|
|
Vector2 positionToGo;
|
|
|
|
Vector2 directionFacing = Vector2.up;
|
|
float rotationVelocity;
|
|
|
|
// Start is called before the first frame update
|
|
void Awake(){
|
|
positionToGo = new Vector2(transform.position.x, transform.position.z);
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update(){
|
|
|
|
|
|
if (!player1)
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.UpArrow))
|
|
{
|
|
positionToGo += Vector2.up;
|
|
directionFacing = Vector2.up;
|
|
GameManager.Instance.movePawn(this, MyTypes.MyDirection._UP);
|
|
}
|
|
else if (Input.GetKeyDown(KeyCode.DownArrow))
|
|
{
|
|
positionToGo += Vector2.down;
|
|
directionFacing = Vector2.down;
|
|
GameManager.Instance.movePawn(this, MyTypes.MyDirection._DOWN);
|
|
}
|
|
else if (Input.GetKeyDown(KeyCode.LeftArrow))
|
|
{
|
|
positionToGo += Vector2.left;
|
|
directionFacing = Vector2.left;
|
|
GameManager.Instance.movePawn(this, MyTypes.MyDirection._LEFT);
|
|
}
|
|
else if (Input.GetKeyDown(KeyCode.RightArrow))
|
|
{
|
|
positionToGo += Vector2.right;
|
|
directionFacing = Vector2.right;
|
|
GameManager.Instance.movePawn(this, MyTypes.MyDirection._RIGHT);
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow))
|
|
AudioManager.instance.PlayOneShot("Pez");
|
|
}
|
|
else
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.W))
|
|
{
|
|
positionToGo += Vector2.up;
|
|
GameManager.Instance.movePawn(this, MyTypes.MyDirection._UP);
|
|
directionFacing = Vector2.up;
|
|
}
|
|
else if (Input.GetKeyDown(KeyCode.S))
|
|
{
|
|
positionToGo += Vector2.down;
|
|
GameManager.Instance.movePawn(this, MyTypes.MyDirection._DOWN);
|
|
directionFacing = Vector2.down;
|
|
}
|
|
else if (Input.GetKeyDown(KeyCode.A))
|
|
{
|
|
positionToGo += Vector2.left;
|
|
GameManager.Instance.movePawn(this, MyTypes.MyDirection._LEFT);
|
|
directionFacing = Vector2.left;
|
|
}
|
|
else if (Input.GetKeyDown(KeyCode.D))
|
|
{
|
|
positionToGo += Vector2.right;
|
|
GameManager.Instance.movePawn(this, MyTypes.MyDirection._RIGHT);
|
|
directionFacing = Vector2.right;
|
|
}
|
|
|
|
if(Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.W))
|
|
AudioManager.instance.PlayOneShot("Pez");
|
|
}
|
|
|
|
//Vector2 currentPosition = Vector2.SmoothDamp(new Vector2(transform.position.x, transform.position.z), positionToGo, ref velocity, .1f, Mathf.Infinity, Time.unscaledDeltaTime);
|
|
//transform.position = new Vector3(currentPosition.x, transform.position.y, currentPosition.y);
|
|
|
|
float targetAngle = Mathf.Atan2(directionFacing.x, directionFacing.y) * Mathf.Rad2Deg;
|
|
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref rotationVelocity, .1f, Mathf.Infinity, Time.unscaledDeltaTime);
|
|
transform.rotation = Quaternion.Euler(0f, angle, 0f);
|
|
}
|
|
|
|
private void oldMovement()
|
|
{
|
|
if (playerManager.playerMoving == null || playerManager.playerMoving == this)
|
|
{
|
|
if (!moving)
|
|
{
|
|
Vector2 movement = Vector2.zero;
|
|
if (Input.GetButtonDown("Up"))
|
|
movement = Vector2.up;
|
|
else if (Input.GetButtonDown("Down"))
|
|
movement = Vector2.down;
|
|
else if (Input.GetButtonDown("Left"))
|
|
movement = Vector2.left;
|
|
else if (Input.GetButtonDown("Right"))
|
|
movement = Vector2.right;
|
|
|
|
if (movement.sqrMagnitude > .1f)
|
|
{
|
|
moving = true;
|
|
directionFacing = movement;
|
|
playerManager.Move(this);
|
|
}
|
|
positionToGo = new Vector2(positionToGo.x + movement.x, positionToGo.y + movement.y);
|
|
}
|
|
else
|
|
{
|
|
Vector2 currentPosition = Vector2.SmoothDamp(new Vector2(transform.position.x, transform.position.z), positionToGo, ref velocity, .1f, Mathf.Infinity, Time.unscaledDeltaTime);
|
|
transform.position = new Vector3(currentPosition.x, transform.position.y, currentPosition.y);
|
|
|
|
if (Vector2.Distance(currentPosition, positionToGo) < .05f)
|
|
{
|
|
transform.position = new Vector3(positionToGo.x, transform.position.y, positionToGo.y);
|
|
moving = false;
|
|
playerManager.Stop();
|
|
}
|
|
}
|
|
}
|
|
|
|
float targetAngle = Mathf.Atan2(directionFacing.x, directionFacing.y) * Mathf.Rad2Deg;
|
|
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref rotationVelocity, .1f, Mathf.Infinity, Time.unscaledDeltaTime);
|
|
transform.rotation = Quaternion.Euler(0f, angle, 0f);
|
|
}
|
|
}
|