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

22.03.06 18:46 작성 조회수 323

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 로 먼저 연습을 할려고 해서 이동하는 걸 만들어 보고
있는데요 지금 계속 중간에 가다가도 멈추고 점프도 될 때도 있고 안될때도 있고 그래서 질문 남깁니다...
빠르게 고쳐서 계속 만들어 보고 싶어요!!

답변 0

답변을 작성해보세요.

답변을 기다리고 있는 질문이에요.
첫번째 답변을 남겨보세요!