using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterMove : MonoBehaviour
{
public Transform cameraTransform;
// ์นด๋ฉ๋ผ ์์ง์ ๋ฐ์์ค๋ cameraTransform ๋ณ์ ์ ์ธ
public CharacterController characterController;
// CharacterController ์ 3D ์ค๋ธ์ ํธ ์ ์ฉ ์ํ characterController ๋ณ์ ์ ์ธ
public float movespeed = 10f;
public float jumppower = 10f;
public float gravity = -20f;
public float yVelocity = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 moveDirection = new Vector3(h, 0, v);
moveDirection = cameraTransform.TransformDirection(moveDirection);
moveDirection *= movespeed;
if(characterController.isGrounded)
{
yVelocity = 0;
if(Input.GetKeyDown(KeyCode.Space))
{
yVelocity = jumppower;
}
}
if(Input.GetKeyDown(KeyCode.F))
{
Debug.Log("GOTO first location");
transform.position = new Vector3(-9, 6, -10);
}
if(transform.position.y < -30f)
{
Debug.Log("Character fell below -30.");
transform.position = new Vector3(-9, 6, -10);
}
yVelocity += (gravity * Time.deltaTime);
moveDirection.y = yVelocity;
characterController.Move(moveDirection * Time.deltaTime);
}
}
ํด๋น ์ฝ๋์์
if(Input.GetKeyDown(KeyCode.F))
{
Debug.Log("GOTO first location");
transform.position = new Vector3(-9, 6, -10);
}
if(transform.position.y < -30f)
{
Debug.Log("Character fell below -30.");
transform.position = new Vector3(-9, 6, -10);
}
์ผ๋จ ์ด๋ถ๋ถ์์ transform.position = new ์ด์ฉ๊ณ ์ด๋ ๊ฒ ํ๋ฉด ์ด๊ธฐ์์น๋ก ํ ๋ ํฌํธ๊ฐ ์๋ฉ๋๋ค.....
๊ทธ๋ฆฌ๊ณ ์ฝ๋ ์ ์ฒด์ ์ธ ๋ถ๋ถ์์ ์๊ทธ๋ฌ๋์ง๋ ๋ชจ๋ฅด๊ฒ ๋๋ฐ
if(characterController.isGrounded)
{
yVelocity = 0;
if(Input.GetKeyDown(KeyCode.Space))
{
yVelocity = jumppower;
}
}
์ฌ๊ธฐ์ yVelocity = 0; ๋ถ๋ถ ์์ ๋ฉด ์ ์๋ฆฌ์์ ์๋ฌด๋ฐ ์์ง์ ์์ด ์ ํ๊ฐ ๋์ง๋ง ๊ณ์ ๊ฐ์ํ๋ yVelocity ๋๋ฌธ์ ๋ํ์ ๊ฐ๋น ๋ฅด๊ฒ ๋๋ ๋ฌธ์ ์ ์ด ์๊ณ ,
๊ทธ๋์ ์ญ์ yVelocity = 0 ์ผ๋ก ์ด๊ธฐํ ์ํค๋๊ฑด ๋ง๋๊ฑฐ๊ฐ์๋ฐ ์ด๋ ๊ฒ๋๋ฉด ์ ์๋ฆฌ์์ ์ ํ๊ฐ ์๋ฉ๋๋ค...
๊ณ ์๋๋ค ๋์์ฃผ์ธ์ฌ,,,
yVelocity๊ฐ์ ๋ก๊ทธ์ฐ์ผ๋ฉด์ ํ์ธํด๋ด 0์ผ๋ก ์ด๊ธฐํํ ๋ค์์ ๊ฐ ์ ๋๋ก ๋ค์ด๊ฐ๋์ง yVelocity += (gravity * Time.deltaTime); ์ด๋ถ๋ถ์ด ๋ฌธ์ ์ผ๊ฑฐ๊ฐ๊ธดํ๋ฐ ๊ทธ๋ฆฌ๊ณ transform.position = new Vector3(-9, 6, -10);์ด ์๋๋๊ฑด ๋ง์ด์๋๋๋ฐ ์บ๋ฆญํฐ ์ปจํธ๋กค๋ฌ๋ ์บ๋ฆญํฐ ๋ฌด๋ธ๋ ์ ๋๋ก ํ๋ ์ด์ด๊ฐ ๊ฐ์ง๊ณ ์๋๊ฑฐ ๋ง์?
์ผ๋จ ์บ๋ฆญํฐ ์์๋ ๋ค์๊ณผ ๊ฐ์์ Player(Head(Camera), Body) Player ์๋ค๊ฐ ๋ฐฉํฅํค๋ก ์์ง์ด๋๊ฑฐ ์คํฌ๋ฆฝํธ ๋ฃ์ด๋จ๊ตฌ ์์ ์์ง์๋ ๋ฃ์์๋๋ฐ ์์ ์์ง์์ Player ์๋ค๊ฐ ๋ฃ์ผ๋๊น ์บ๋ฆญํฐ ์ ์ฒด๊ฐ ๊ธฐ์ธ์ฌ์ ธ์ ์นด๋ฉ๋ผ๋ก ๋ฃ์์ต๋๋ค ใ ใ
yVelocity ๊ฐ์ ์ธ์คํํฐ์ฐฝ์์ ์ค์๊ฐ์ผ๋ก ๋จ๋๋ฐ 0.xxxx ์ด๋ ๊ฒ ๊ณ์ ๋ณํฉ๋๋น ์๋ง ์ค๋ ฅ๊ฐ์๋๋๋ฌธ์ธ๊ฒ๊ฐ์์
์ผ๋จ yVelocity = 0; ๋ if(Input.GetKeyDown(KeyCode.Space)) ์ ์(yVelocity = jumppower; ์ ์์ชฝ) ์ ๋ฃ์ด๋๋ฉด ์ ํ๋ ํด๊ฒฐ ๋ ๊ฒ ๊ฐ์์ if(characterController.isGrounded) ๋ ์บ๋ฆญํฐ๊ฐ ๋ ์ ๋ถ์ด์๋ ํ ๊ณ์ Update ๋ ๋ง๋ค ๋ถ๋ฌ์ ์ง ํ ๋ฐ ๊ทธ๋ผ ์ ํ๋ฅผ ํ ์งํ ๋ค์ ํ๋ ์์๋ ํธ์ถ๋์ด์ ๋ ์ ๋ฒ์ด๋ ์ ์๋ ์ํ์ธ ๊ฒ ๊ฐ์์
ํ ๋ ํฌํธ๊ฐ ์๋๋ ์ด์ ๋ ๊ฒ์ ์ค๋ธ์ ํธ์ ๋ถ์ด์๋ CharacterController ๋๋ฌธ์ผ๊ฑฐ์์ CharacterController ๊ฐ ์ ์ ์ผ๋ก transform ์ ์ ์ดํ๊ณ ์์ด์ ์คํฌ๋ฆฝํธ์์ ๋ฐ๋ก transform์ ๋ฐ๊พธ๋๊ฑธ ๋ฌด์ํฉ๋๋ค characterController.enabled = false; transform.position = new Vector3(-9, 6, -10); characterController.enabled = true; ํํ๋ก ์ฐ๋ฉด ์ํ๋๋๋ก ๋์ ํ ๊ฑฐ์์ค
๊ฐ์ฌํฉ๋๋ค!!!! ๊ณ์ ๋ถ์ก๊ณ ๊ณต๋ถํด๋ณด๋ charactercontroller ๋ณด๋ค rigidbody๋ฅผ ์จ์ผํ ๊ฒ๊ฐ๊ธฐ๋ ํ๋ค์....