using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class PlayerMovement : MonoBehaviourPun
{
private Vector3 movement;
private Rigidbody playerRigidbody;
public float playerSpeed;
public float rotationSpeed;
private float h;
private float v;
public bool moving; //basic movement
public bool acting; //dash , skill attacks, etc
public bool tumbled; //tumbled when hit by enemy
public bool alive;
private const float SPEED_DEFAULT = 5f;
private const float ROTATION_DEFAULT = 4f;
private const float ADDFORCE_DEFAULT = 1000f;
public static bool controllerActive;
void Awake()
{
playerRigidbody = this.gameObject.GetComponent<Rigidbody>();
}
void Start()
{
controllerActive = true;
alive = true;
playerSpeed = SPEED_DEFAULT;
rotationSpeed = ROTATION_DEFAULT;
}
void Update()
{
if (photonView.IsMine == false && PhotonNetwork.IsConnected == true)
return;
if (controllerActive)
{
if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
{
h = JoystickManager.instance.x;
v = JoystickManager.instance.y;
}
else //for PC in editor
{
if (JoystickManager.instance.joystickActivation == true)
{
h = JoystickManager.instance.x;
v = JoystickManager.instance.y;
}
else
{
h = Input.GetAxisRaw("Horizontal");
v = Input.GetAxisRaw("Vertical");
}
}
}
}
void LateUpdate()
{
if (photonView.IsMine == false && PhotonNetwork.IsConnected == true)
return;
if (alive && controllerActive)
{
if (h != 0 || v != 0)
{
if (!tumbled)
{
moving = true;
Move(h, v);
Turn(h, v);
}
}
else
{
moving = false;
}
}
}
void Move(float hor, float ver)
{
Vector3 targetDirection = new Vector3(hor, 0f, ver);
targetDirection = CameraManager.instance.cVC.transform.TransformDirection(targetDirection);
targetDirection.y = 0.0f;
movement = targetDirection.normalized * playerSpeed * Time.deltaTime;
playerRigidbody.MovePosition(transform.position + movement);
}
void Turn(float hor, float ver)
{
if (!acting)
rotationSpeed = ROTATION_DEFAULT + 1f;
else
rotationSpeed = ROTATION_DEFAULT - 1f;
Vector3 targetDirection = new Vector3(hor, 0f, ver);
targetDirection = CameraManager.instance.cVC.transform.TransformDirection(targetDirection);
targetDirection.y = 0.0f;
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
Quaternion newTargetRotation = Quaternion.Lerp(playerRigidbody.rotation, targetRotation, rotationSpeed * Time.deltaTime);
playerRigidbody.MoveRotation(newTargetRotation);
}
}