반응형
- 코드 작성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Transform player;
public Rigidbody rigid;
public Transform cameraArm;
public float moveSpeed = 5.0f;
public float mouseSpeed = 2.0f;
public float jumpPower = 10f;
private void FixedUpdate()
{
Move();
LookAround();
Jump();
OnReset();
}
private void Move()
{
Vector2 moveInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
bool isMove = moveInput.magnitude != 0;
if (isMove)
{
Vector3 lookForward = new Vector3(cameraArm.forward.x, 0f, cameraArm.forward.z).normalized;
Vector3 lookRight = new Vector3(cameraArm.right.x, 0f, cameraArm.right.z).normalized;
Vector3 moveDir = lookForward * moveInput.y + lookRight * moveInput.x;
player.forward = moveDir;
transform.position += moveDir * Time.deltaTime * moveSpeed;
}
}
private void LookAround()
{
Vector2 mouseDelta = new Vector2(Input.GetAxis("Mouse X") * mouseSpeed, 0);
Vector3 camAngle = cameraArm.rotation.eulerAngles;
float x = camAngle.x - mouseDelta.y;
if (x < 180f)
{
x = Mathf.Clamp(x, -1f, 70f);
}
else
{
x = Mathf.Clamp(x, 335f, 361f);
}
cameraArm.rotation = Quaternion.Euler(x, camAngle.y + mouseDelta.x, camAngle.z);
}
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rigid.AddForce(Vector3.up * jumpPower, ForceMode.Impulse);
Debug.Log("Jump");
}
}
void OnReset()
{
if (Input.GetKeyDown(KeyCode.R))
{
transform.position = Vector3.zero;
cameraArm.rotation = Quaternion.identity;
Debug.Log("Reset");
}
}
}
반응형