2023-03-14 00:59:59 +01:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class PlayerMovementController : MonoBehaviour
|
|
|
|
{
|
|
|
|
[SerializeField]
|
|
|
|
private PlayerAnimationController animcontroller;
|
|
|
|
|
|
|
|
private float x = 0;
|
|
|
|
private float y = 0;
|
|
|
|
private float mouseX = 0;
|
|
|
|
|
|
|
|
private bool isRunning = false;
|
|
|
|
[SerializeField]
|
|
|
|
private CharacterController ccontroller;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private CameraController cameraController;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private float speed = 1f;
|
|
|
|
[SerializeField]
|
|
|
|
private float sideSpeed = 0.5f;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private float animatedRotationSpeed = 5f;
|
2023-04-08 07:23:11 +02:00
|
|
|
[SerializeField]
|
|
|
|
private Camera cam;
|
2023-03-14 00:59:59 +01:00
|
|
|
|
2023-04-10 04:30:54 +02:00
|
|
|
[HideInInspector]
|
|
|
|
public bool AllowRotation = false;
|
2023-03-14 00:59:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Vector3 lookingDirectionVector;
|
|
|
|
private void GetMovementOld()
|
|
|
|
{
|
|
|
|
x = Input.GetAxis("Horizontal");
|
|
|
|
y = Input.GetAxis("Vertical");
|
|
|
|
isRunning = (Mathf.Abs(y) > 0.1f || Mathf.Abs(x) > 0.1f);
|
|
|
|
}
|
|
|
|
private void MovePlayer()
|
|
|
|
{
|
|
|
|
Vector3 movement = Vector3.zero;
|
|
|
|
movement += transform.forward * Mathf.Abs(y) * Time.deltaTime * speed;//+(transform.right*x*Time.deltaTime*speed);
|
|
|
|
//movement += transform.right * Mathf.Abs(x) * Time.deltaTime * sideSpeed;
|
|
|
|
movement += transform.forward * Mathf.Abs(x) * Time.deltaTime * speed;
|
2023-03-21 19:49:47 +01:00
|
|
|
movement += Vector3.down * 9.8f;
|
2023-03-14 00:59:59 +01:00
|
|
|
ccontroller.Move(movement);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetSpeed(float speed)
|
|
|
|
{
|
|
|
|
this.speed = speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void GetJumpOld()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
2023-04-10 04:30:54 +02:00
|
|
|
AllowRotation = Input.GetMouseButton(1);
|
2023-03-14 00:59:59 +01:00
|
|
|
GetMovementOld();
|
|
|
|
MovePlayer();
|
|
|
|
|
|
|
|
animcontroller.Animate(new Vector2(x, y), false,isRunning);
|
|
|
|
|
|
|
|
this.lookingDirectionVector = ((cameraController.Forward * y) + cameraController.Right * x).normalized;
|
2023-04-10 04:30:54 +02:00
|
|
|
if (isRunning && !Input.GetMouseButtonDown(1)||AllowRotation)
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
2023-04-10 04:30:54 +02:00
|
|
|
if (AllowRotation && x == 0 && y == 0)
|
|
|
|
{
|
|
|
|
this.lookingDirectionVector = cameraController.Forward.normalized;
|
|
|
|
}
|
|
|
|
|
2023-03-14 00:59:59 +01:00
|
|
|
SlowLookAt(this.lookingDirectionVector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SlowLookAt(Vector3 targetVector)
|
|
|
|
{
|
|
|
|
Vector3 relativePos = targetVector;
|
|
|
|
Quaternion toRotation = Quaternion.LookRotation(relativePos);
|
|
|
|
|
|
|
|
transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, animatedRotationSpeed * Time.deltaTime);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|