inflearn logo
강의

講義

知識共有

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

解決済みの質問

459

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

Spider와 Monster 구현 방식을 구분한 이유

1

17

1

Sprute Sheet에 대해서.

0

19

1

ResourceManager 클래스 관련 질문

1

35

2

FBX chan 모델

0

26

1

첨부된 수업자료와 강의 내용이 다릅니다.

0

53

2

몬스터 HP 게이지바 이동

0

23

1

TextMeshPro용 커스텀 한글 2350자.txt 파일은 어디에 있나요?

0

28

1

02-02 NavMesh 응용 캐릭터 멈춤 문제

0

34

1

02-02 NavMesh 응용 캐릭터 멈춤 문제

0

46

2

C#에서의 RAII

0

67

3

Scene 뷰에서 Enemy Entity가 보이지 않는 문제

0

50

2

Lessons 3 & 4 don't have subtitles unfortunately

0

40

1

클로드코드 + 유니티 컨텐츠만들기 응용에 대하여 질문드립니다.

0

74

1

매니저 관련 질문이 있습니다.

0

73

2

Instantiate

0

52

2

유니티 허브 다운로드

1

62

2

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

0

78

2

UserDataManager 클래스 hasSaveError 처리

0

47

2

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

0

44

2

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

0

56

1

싱글턴패턴

0

48

2

코드 관련 질문

0

53

2

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

0

48

2

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

0

39

1