unity 2d 이동 좀 알려줘용....
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
싱글턴패턴
0
13
1
코드 관련 질문
0
21
2
섹션7 수업자료 업로드 부탁드립니다.
0
24
2
Dictionary Key를 int에서 string으로 변경한 이유에 대한 문의
0
20
1
UI 기능 관련 질문이 있습니다!
0
37
2
03-01 (16. CharacterController)
0
32
2
TLS 질문드립니다.
0
43
2
Task 구현 28:36 Equals 에서 잘 모르는 부분이 있습니다.
0
27
2
SpinLock과 컨텍스트스위칭에 대해 질문 남겨요.
0
48
2
픽셀 좌표 스크린 좌표
0
33
0
Locomotion랑 Turn 이 꼭 부모 자식 관계일 필요가 있나요?
0
25
1
LobbyUIController의 백키가 사라졌는데 왜그런건가요?
0
42
2
무조건 타이틀 씬부터 시작해야하나요?
0
41
1
BaseUI, UIManager
0
51
3
프로젝트 완성본 문의
0
41
2
Unity Span
0
76
2
씬 배치 구조에서 Addressables를 어떻게 적용해야 하는지 궁금합니다.
0
55
1
39. Main화면 추가 UI 에서 자료는 어디서 받나요??
0
38
1
Scene 전환에서
0
29
1
전체적으로 코드 읽는게 굉장히 오래 걸리네요...
0
75
2
사전학습 강의
0
57
1
3d프로젝트에 적용해도 무방한가요?ㅁ
0
71
2
VR기기 실행시 컨트롤러 rotation이 계속 거꾸로 초기화됩니다.
0
69
2
추후에 이 내용에 대해서 실질적인 게임개발에 들어가면서 복습하는 시간이 있을까요.
0
91
2

