Web

스터디노트 01 - code: 이동,점프,대수

또롱또 2020. 9. 28. 16:48
728x90

유니티

RigidBody

 - 게임오브젝트를 엔진에서 제어하도록 해주는 컴포넌트

 - Mass: 질량, 중력영향x 다른 RigidBody끼리 충돌했을때 반응을 어떻게 할지 정함

 - Drag: 공기저항, 값이 작을수록 오브젝트가 무거워보임

          ex) 점프했을때 값이 크면, 공기저항을 덜받아서, 깃털처럼 올라갔다 내려옴

 - Angular Drag: 회전할때 공기저항

 - Is Kinematic: 물리엔진이 아닌 게임오브젝트 로직에 따라서 이 게임오브젝트를 이동을 할지 결정

 - Interpolate: 물리엔진에서의 애니메이션이 자연스럽게 보관을 할것인지

 - Collision detection: 충돌처리를 연속적으로 할지, 아니면 특정한 경우에만 할지

 

 

c#

#region

#endregion 기능을 이용하면 보기 싫은 부분을 folding이 가능하다.

 

FixedUpdate() vs Update()

FixedUpdate()는 게임의 프레임과 상관없이 고정적으로 호출 --> movment에는 fixedDeltaTime.

Update()는 매 프레임마다 호출

 

Raycast - 광선을 쏜다라는 의미, 직선으로 광선을 쏴서, 광선에 맞은 물체의 정보를 가져올 수도 있음.

RaycastHit - 매개변수를 넣으면 Raycast에 맞은 오브젝트의 정보를 가져옴

 

아래 점프코드:

 - 플레이어에서 아래로 레이케스트를 쏴서, 레이캐스트 힛에 그라운드가 범위보다 크면 점프를 못하게 막음

 

 

code

public class RigidBodyCharater : MonoBehaviour
{
    #region Variables
    //basic variables about the movements
    public float speed = 5.0f;
    public float jumpHeight = 2.0f;
    public float dashDistance = 5.0f;

    //to get Rigidbody from the unity
    private Rigidbody rigidbody;

    //movement(vector3)
    private Vector3 inputDirection = Vector3.zero;

    //prevent double jump
    private bool isGround = false;
    public LayerMask groundLayerMask;
    public float groundCheckDistance = 0.3f;
    #endregion

    // Start is called before the first frame update
    void Start()
    {
        //to get Rigidbody from the unity
        rigidbody = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        CheckGroundStatus();

        //user input - basic movments
        inputDirection = Vector3.zero;
        inputDirection.x = Input.GetAxis("Horizontal");
        inputDirection.z = Input.GetAxis("Vertical");

        //actual code about user movements
        if(inputDirection != Vector3.zero)
        { 
            transform.forward = inputDirection;
        }

        //user input - jump
        if (Input.GetButtonDown("Jump") && isGround)
        {
            Vector3 jumpVelocity = Vector3.up * Mathf.Sqrt(jumpHeight * -2.0f * Physics.gravity.y);
            rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
        }

        //user input - dash
        if (Input.GetButtonDown("Dash"))
        {
            Vector3 dashVelocity = Vector3.Scale(transform.forward, dashDistance * new Vector3((Mathf.Log(1f / (Time.deltaTime * rigidbody.drag + 1)) / -Time.deltaTime), 
                0, (Mathf.Log(1f / (Time.deltaTime * rigidbody.drag + 1)) / -Time.deltaTime)));
            rigidbody.AddForce(dashVelocity, ForceMode.VelocityChange);
        }
    }

    void FixedUpdate()
    {
        rigidbody.MovePosition(rigidbody.position + inputDirection * speed * Time.fixedDeltaTime);
    }

    #region Methods
    //to check if player is on the ground
    void CheckGroundStatus()
    {
        RaycastHit raycastHit;
        if (Physics.Raycast(transform.position + (Vector3.up * 0.1f), Vector3.down, out raycastHit, groundCheckDistance, groundLayerMask))
        {
            isGround = true;
        }
        else
        {
            isGround = false;
        }
    }
    #endregion Methods

}
728x90