커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
캐릭터 움직이기
undefined주차
북마크
채*환
댓글
5
추천
0
조회수
17
조회수
17
답변 완료

* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.

* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.


움직임에 관해서 구글링을 하다가 부스팅 움직임에 대해서 테스트 해보고 있었습니다.

Mathf.Lerp를 사용하면 부드럽게 속도를 변화시킨다길래 사용하였고,

targetVelocity를 통해서 목표로 하는 속도를 쉬프트 및 마우스 우클릭을 통해 변화를 주고 싶었습니다.

public으로 하면 Inspector창의 스크립트에 값이 표시되기에 확인을 위해서 선언하였습니다.

방향 전환은 부드럽게 되지만, 쉬프트를 누르기만 하면 position.x 가 0으로 이동하고 속도가 급격히 내려가 움직이지 않습니다.


코드는 유니티 스토어 에셋의 StarterAssets의 ThirdPersonController 코드의 Move() 참고하였습니다.



보고 계신 화면 전체를 캡처해 주시면, 튜터님들이 빠르게 상황을 이해할 수 있어요.

스파르타 즉문즉답




작성한 코드 및 에러 메세지

오류 발생 시, 작성한 코드 전체와 에러 메시지를 첨부해 주세요.

Tip 1) </> 아이콘을 눌러 코드박스를 만들어 보세요.

Tip 2) Ctrl+A(맥의 경우 Command+A) 단축키로 코드를 한 번에 선택할 수 있어요!


public class RtanPlayer: MonoBehaviour
{
    // Start is called before the first frame update
    float velocity;
    float direction;
    float targetVelocity;
    public float currentVelocity;
    float pastPosition;
    void Start()
    {
        pastPosition = transform.position.x;
        currentVelocity = 0.0f;
        velocity = 0.0f;
        direction = 1.0f;
    }


    // Update is called once per frame
    void Update()
    {
        targetVelocity = (Input.GetMouseButton(1) || Input.GetKey(KeyCode.LeftShift)) ? 1.5f : 1.0f; 
        currentVelocity = (transform.position.x - pastPosition)/Time.deltaTime;
        if (transform.position.x <= -2.6f || transform.position.x >= 2.6f)
        {
            direction = direction * (-1);
            transform.localScale = new Vector3(direction, 1.0f, 1.0f);
        }
        if (Input.GetButtonUp("Jump") || Input.GetMouseButtonUp(0) )
        {
            direction = direction * (-1);
            transform.localScale = new Vector3(direction, 1.0f, 1.0f);
        }


        float speedOffset = 0.1f;
        float speedChangeRate = 0.5f;


        if ( velocity <= targetVelocity - speedOffset || velocity >= targetVelocity + speedOffset)
            velocity = Mathf.Lerp(targetVelocity, currentVelocity, speedChangeRate);
        else
            velocity = targetVelocity;
        velocity = Mathf.Round(velocity * 1000.0f) / 1000.0f;
        transform.position += new Vector3(direction * velocity * Time.deltaTime, 0.0f, 0.0f);
    }


취소
 공유
취소
댓글 0
댓글 알림
나의얼굴