아카이브
플레이어 이동 본문
임시로 만드는 플레이어라 3D 오브젝트 캡슐로 플레이어 설정
playerMove.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMove : MonoBehaviour
{
public CharacterController controller;
public float speed = 6f;
public float gravity = -9.81f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public float jumpHeight = 3f;
private float jumpMultiplier = 1f;
Vector3 velocity;
bool isGrounded;
void Jump()
{
if ((Input.GetButtonDown("Jump")) && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity * jumpMultiplier);
jumpMultiplier = 1f;
}
}
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
speed = 6f;
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
Jump();
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Player 안에 groundCheck 오브젝트 생성 후 플레이어 밑바닥에 붙여줌
카메라를 플레이어 머리 부분에 달아준다
camMove.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camMove : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
bool canFreeMouse = false;
void Update()
{
if(canFreeMouse == false)
{
Cursor.lockState = CursorLockMode.Locked;
}
else
{
Cursor.lockState = CursorLockMode.None;
}
if(Input.GetKey(KeyCode.Escape))
{
canFreeMouse = true;
}
else
{
canFreeMouse = false;
}
float MouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float MouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= MouseY;
xRotation = Mathf.Clamp(xRotation, -89f, 89f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * MouseX);
}
}
'3D 콘텐츠 제작' 카테고리의 다른 글
3D 프로젝트 발표 (0) | 2023.10.12 |
---|---|
포탈 텔레포트 (0) | 2023.09.25 |
포탈 만들기 (0) | 2023.09.25 |
3D 프로젝트로 제작할 게임 (0) | 2023.09.22 |