본문 바로가기
『 C 』Languege/게임엔진

unity 1장 움직이기 예제

by Play IT 2019. 6. 9.
반응형

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{

    [SerializeField]            //인스펙터창에서 수정할수 있게 해준다.
    private float walkSpeed;


    [SerializeField]
    private Rigidbody myRigid;             //보이는 것 이상의 물체 역할을 한다.



    // Start is called before the first frame update
    void Start()
    {


        myRigid = GetComponent();   // 시리얼필드와 비슷하다.
        

    }

    // Update is called once per frame
    void Update()   //1초에 60프레임이 작동한다. 실시간 움직임
    {
        Move();



    }

    private void Move()
    {

        float _moveDirX = Input.GetAxisRaw("Horizontal");  // 키보드의 좌우 화살표 혹은 ad 가 입력된다 a => 1 d=> -1 
        float _moveDirZ = Input.GetAxisRaw("Vertical");  // ws 정면과 뒤

        Vector3 _moveHorizontal = transform.right * _moveDirX;   // (1,0,0) transform의 위치를 움직이겠다 
        Vector3 _moveVertical = transform.forward * _moveDirZ;  //(0,0,1)

        Vector3 _velocity = (_moveHorizontal + _moveVertical).normalized * walkSpeed;    //(1,0,1) => 2 노멀라이즈 (0.5,0,0.5) => 1 대각선 이동이 가능하다.

        myRigid.MovePosition(transform.position + _velocity * Time.deltaTime);  // 순간이동하는 것처럼 이동하기에 Time을 추가하여 자연스럽게 한다. 1초마다 이동시킨다 이다.


    }
}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{

    [SerializeField]            //인스펙터창에서 수정할수 있게 해준다.
    private float walkSpeed;

    [SerializeField]
    private float lookSensitivity; // 화면의 민감도

    [SerializeField]
    private float cameraRotationLimit; //마우스로 고개를 들때 어느 각도까지만 올라가게 한다.
    private float currentCameraRotationX = 0f; //정면을 바라본다.

    [SerializeField]
    private Camera theCamera;

    private Rigidbody myRigid;             //보이는 것 이상의 물체 역할을 한다.



    // Start is called before the first frame update
    void Start()
    {

        //theCamera = FindObjectOfType(); // 하위 객체에서 카메라를 찾아버린다. 
        myRigid = GetComponent();   // 시리얼필드와 비슷하다.
        

    }

    // Update is called once per frame
    void Update()   //1초에 60프레임이 작동한다. 실시간 움직임
    {
        Move();
        CameraRotation();
        CharacterRotation();


    }

    private void Move()
    {

        float _moveDirX = Input.GetAxisRaw("Horizontal");  // 키보드의 좌우 화살표 혹은 ad 가 입력된다 a => 1 d=> -1 
        float _moveDirZ = Input.GetAxisRaw("Vertical");  // ws 정면과 뒤

        Vector3 _moveHorizontal = transform.right * _moveDirX;   // (1,0,0) transform의 위치를 움직이겠다 
        Vector3 _moveVertical = transform.forward * _moveDirZ;  //(0,0,1)

        Vector3 _velocity = (_moveHorizontal + _moveVertical).normalized * walkSpeed;    //(1,0,1) => 2 노멀라이즈 (0.5,0,0.5) => 1 대각선 이동이 가능하다.

        myRigid.MovePosition(transform.position + _velocity * Time.deltaTime);  // 순간이동하는 것처럼 이동하기에 Time을 추가하여 자연스럽게 한다. 1초마다 이동시킨다 이다.


    }

    private void CharacterRotation() //좌우 캐릭터 회전
    {
        float _yRotation = Input.GetAxisRaw("Mouse X");
        Vector3 _characterRotationY = new Vector3(0f, _yRotation, 0f) * lookSensitivity;

        myRigid.MoveRotation(myRigid.rotation * Quaternion.Euler(_characterRotationY)); // myRigid.rotation 회전반경에 
       // Debug.Log(myRigid.rotation);
        // Debug.Log(myRigid.rotation.eulerAngles);
    }

    private void CameraRotation() //상하 카메라 호지ㅓㄴ
    {
        float _xRotation = Input.GetAxisRaw("Mouse Y"); //마우스는 X, Y 만 있다
        float _cameraRotationX = _xRotation * lookSensitivity; //어느정도 천천히 움직이게 만든다 감도
        currentCameraRotationX -= _cameraRotationX;
        currentCameraRotationX = Mathf.Clamp(currentCameraRotationX, -cameraRotationLimit, cameraRotationLimit); //고정된다

        theCamera.transform.localEulerAngles = new Vector3(currentCameraRotationX, 0f, 0f); //카메라를 적용시켜줘야지

    }

}

rigidibody 를 추가하여

x y 를 안움직이게 체크해준다.

반응형

'『 C 』Languege > 게임엔진 ' 카테고리의 다른 글

Windows 10 에서 SCP : SL 서버 열기  (0) 2020.09.01

댓글