그 문제를 없애기 위해 h, v를 전역변수로 뺀다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody rigid;
public int JumpPower = 10;
public int MoveSpeed = 10;
public float h = 0.0f;
public float v = 0.0f;
private bool IsJumping;
// Start is called before the first frame update
void Start()
{
rigid = GetComponent<Rigidbody>();
IsJumping = false;
}
// Update is called once per frame
void Update()
{
Move();
Jump();
}
private void Move()
{
if (!IsJumping)
{
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
}
transform.Translate(new Vector3(h, 0, v) * MoveSpeed * Time.deltaTime);
}
void Jump()
{
if(Input.GetKeyDown(KeyCode.Space))
{
if(!IsJumping)
{
IsJumping = true;
rigid.AddForce(Vector3.up * JumpPower, ForceMode.Impulse);
}
else
{
return;
}
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Ground")) {
IsJumping = false;
}
}
}
'컴퓨터&빅데이터&AI > 프로그래밍및컴퓨터공학교재추천' 카테고리의 다른 글
유니티 강의 중에 좋은거 하나 발견 (0) | 2022.01.12 |
---|---|
(PyTorch를 활용한) 강화학습/심층강화학습 실전 입문 (0) | 2021.06.28 |
허프 변환 (0) | 2021.06.23 |
유니티짱으로 배우는 유니티 5 3D 게임 제작 입문 (0) | 2021.05.17 |
게임으로 익히는 코딩 알고리즘 (0) | 2021.05.07 |
댓글