inflearn logo
강의

Khóa học

Chia sẻ kiến thức

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

Đã giải quyết

486

song050114

2 câu hỏi đã được viết

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

Câu trả lời 0

강의에서 제공된 게임 소스코드의 상업적 이용 관련 문의

0

22

2

학습 진도 관련 질문입니다.

0

37

2

리뷰이벤트 참여하고 싶어요~!

0

37

1

마지막 74강 자료 못 받나요

0

38

1

Albedo 적용이 안되네요

0

44

2

안녕하세요 Rookiss님 기본기를 다지고 있는 대학생입니다.

0

233

2

스크립트의 참조 설정

0

104

2

코드 스크립트 완성본 못 받나요 ?

0

115

2

'Canvas 설정과 배경 설정’ 부분에 대해 질문드립니다.

1

94

1

02-03 벽 마찰력 설정 material 적용 부분

0

101

1

영상이 안나옵니다. 그냥 빈공간이에요

1

138

3

layout group의 종류 3가지 실무 적합활용이 궁금합니다.

0

101

1

수박게임 자료 받았는데 plante05, 06이 없습니다.

0

100

1

14강 녹음이 안되어 있습니다

0

99

1

유니티webgl 과 안티그래비티를 협업시키고 싶습니다.

0

119

2

소스를 받을 수 있는 링크가 있을까요?

0

119

2

프로덕션 부분 사용 불가

0

123

2

접근지정자

0

109

2

in-app purchasing 버튼의 install 버튼이 비활성화되어있을때 어떻게 해결해야하나요 ?

0

145

2

유니티 제외 설치한 프로그램들 및 파일 삭제 방법

0

170

1

깃허브에서 콤피유아이 매니저 설치하는게 안됩니다.

0

173

2

5강, 오류 수정과 관련해서

0

155

2

컴퓨터를 껐다가 클로드 코드 다시 키는 방법 알려주세요.

0

138

1

MA-POCA 알고리즘 응용 질문

0

94

1