inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

unity 2d 이동 좀 알려줘용....

해결된 질문

454

song050114

작성한 질문수 2

0

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

public class Move : MonoBehaviour
{
    private float Speed;
    public float WalkSpeed;
    public float RunSpeed;
    private bool isRunning = false;
    public float JumpForce;
    private bool isJumpping = false;

    Rigidbody2D rb;
    SpriteRenderer sr;
    Animator ani;
   
    void Start()
    {
        Speed = WalkSpeed;

        rb = GetComponent<Rigidbody2D>();
        ani = GetComponent<Animator>();
        sr = GetComponent<SpriteRenderer>();
    }

    void FixedUpdate()
    {
        MoveControl();
        TryRun();
        Running();
        TryJump();
    }



    private void MoveControl()
    {
        float hor = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(hor * Speed, rb.velocity.y);

        //MoveStop
        if (Input.GetButtonUp("Horizontal"))
        {
            rb.velocity = new Vector2(rb.velocity.normalized.x * 0.5f, rb.velocity.y);
        }

        //MoveSpeed
        if (rb.velocity.x > Speed)
        {
            rb.velocity = new Vector2(Speed, rb.velocity.y);
        }
        else if (rb.velocity.x < Speed*(-1))
        {
            rb.velocity = new Vector2(Speed*(-1), rb.velocity.y);
        }

        //Animation
        if (rb.velocity.normalized.x == 0)
        {
            ani.SetBool("isWalking", false);
        }
        else
        {
            ani.SetBool("isWalking", true);
        }

        //Sprite Flip
        if (Input.GetButtonDown("Horizontal"))
        {
            sr.flipX = Input.GetAxisRaw("Horizontal") == -1;
        }
    }
    private void TryRun()
    {
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            isRunning = true;
        }
        else if (Input.GetKeyUp(KeyCode.LeftShift))
        {
            isRunning = false;
        }
    }
    private void Running()
    {
        if (isRunning == true)
        {
            Speed = RunSpeed;
            isRunning = false;
        }
        else if (isRunning == false)
        {
            Speed = WalkSpeed;
            isRunning = true;
        }
    }
    private void TryJump()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isJumpping == false)
        {
            rb.velocity = Vector2.up * JumpForce;
            isJumpping = true;
        }
        else if (Input.GetKeyUp(KeyCode.Space))
        {
            isJumpping = false;
        }
    }
}
제가 이번에 이제 유니티를 시작하게 되었는데요 유튜브를 여러 개 찾아 보면서 계속 연습을 하면서 이번에
제작에 들어가게 되었습니다 처음 시작이여서 2D 로 먼저 연습을 할려고 해서 이동하는 걸 만들어 보고
있는데요 지금 계속 중간에 가다가도 멈추고 점프도 될 때도 있고 안될때도 있고 그래서 질문 남깁니다...
빠르게 고쳐서 계속 만들어 보고 싶어요!!

unity 유니티 unity2d 유니티2d

답변 0

유니티 허브 다운로드

1

20

2

비쥬얼 스튜디오에서 unity연결이 없습니다.

0

39

2

UserDataManager 클래스 hasSaveError 처리

0

22

2

제공해주신 자료에 스크립트들이 빠져있습니다

0

22

2

플레이어를 왜 ECS로 만드는 건가요?

0

25

1

싱글턴패턴

0

29

2

코드 관련 질문

0

30

2

섹션7 수업자료 업로드 부탁드립니다.

0

34

2

Dictionary Key를 int에서 string으로 변경한 이유에 대한 문의

0

25

1

UI 기능 관련 질문이 있습니다!

0

46

2

03-01 (16. CharacterController)

0

36

2

TLS 질문드립니다.

0

51

2

Task 구현 28:36 Equals 에서 잘 모르는 부분이 있습니다.

0

31

2

SpinLock과 컨텍스트스위칭에 대해 질문 남겨요.

0

55

2

픽셀 좌표 스크린 좌표

0

35

0

Locomotion랑 Turn 이 꼭 부모 자식 관계일 필요가 있나요?

0

29

1

LobbyUIController의 백키가 사라졌는데 왜그런건가요?

0

45

2

무조건 타이틀 씬부터 시작해야하나요?

0

43

1

BaseUI, UIManager

0

58

3

프로젝트 완성본 문의

0

42

2

Unity Span

0

81

2

씬 배치 구조에서 Addressables를 어떻게 적용해야 하는지 궁금합니다.

0

60

1

39. Main화면 추가 UI 에서 자료는 어디서 받나요??

0

40

1

Scene 전환에서

0

34

1