37 lines
1.1 KiB
C#
37 lines
1.1 KiB
C#
using UnityEngine;
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
public GameObject DougBody;
|
|
public float walkSpeed;
|
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
RotatePlayerTowardMouse();
|
|
Walk();
|
|
}
|
|
|
|
void Walk()
|
|
{
|
|
transform.position += DougBody.transform.forward * Input.GetAxis("Vertical") * walkSpeed * Time.deltaTime;
|
|
transform.position += DougBody.transform.right * Input.GetAxis("Horizontal") * walkSpeed * Time.deltaTime;
|
|
}
|
|
|
|
void RotatePlayerTowardMouse()
|
|
{
|
|
Vector2 positionOnScreen = Camera.main.WorldToViewportPoint(DougBody.transform.position);
|
|
Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
|
|
|
|
float angle = Mathf.Atan2(positionOnScreen.y - mouseOnScreen.y, positionOnScreen.x - mouseOnScreen.x) * Mathf.Rad2Deg;
|
|
|
|
DougBody.transform.rotation = Quaternion.Euler(new Vector3(0f, -(angle + 90), 0f));
|
|
}
|
|
}
|