2014-10-11 3 views
0

나는 퐁 튜토리얼을 따라 왔지만 문제가 있습니다.유니티에서 키 입력이 작동하지 않습니다

화살표를 누를 때 라켓이 움직이지 않습니다.

using UnityEngine; 
using System.Collections; 

public class MoveRacket : MonoBehaviour 
{ 
    // up and down keys (to be set in the Inspector) 
    public KeyCode up; 
    public KeyCode down; 

    void FixedUpdate() 
    { 
     // up key pressed? 
     if (Input.GetKey(up)) 
     { 
      transform.Translate(new Vector2(0.0f, 0.1f)); 
     } 

     // down key pressed? 
     if (Input.GetKey(down)) 
     { 
      transform.Translate(new Vector2(0.0f, -0.1f)); 
     } 
    } 
} 

그리고 관리자는 이미지에 키를 할당했습니다.

+0

ifs에서 디버깅을 시도 했습니까? 무언가를 인쇄하여 조건에 들어가는 지 확인하십시오. –

답변

0

좋아, 그럼 당신이 확인해야 할 것은 프로젝트 설정에서 KeyCode가 정의되어 있는지 확인하는 것입니다.

최선은 키 입력을 감지하기 위해 다음과 같은 코드를 사용 :

void Update() { 
    if (Input.GetButton("Fire1") && Time.time > nextFire) { 
     nextFire = Time.time + fireRate; 
     Instantiate(projectile, transform.position, transform.rotation); 
    } 
} 

출처 : http://docs.unity3d.com/ScriptReference/Input.GetButton.html 는 또한 나는 다음 자습서를 시청하는 것이 좋습니다가 : https://www.youtube.com/watch?v=JgY5YxNHxtw 그것은 매우 명확하게 모든 것을 설명합니다. 당신은 C#을 내가 당신을 얻을 생각

+0

어떻게해야합니까? – user1627724

+0

나는 그것을 업데이트했다;) 나는 그것이 도움이 될 것이라고 확신 비디오를 확인하십시오. –

0

를 사용하는 것처럼 보인다 무효화 할 수있는 기능을 변경 :

편집이)

은 당신을 도와 바랍니다. Inspector에서 키 코드를 지정하지 않아도됩니다. 스크립트에서 직접 액세스 할 수 있습니다. 스크립트를 단순화해야합니다. 대신이의

:

using UnityEngine; 
using System.Collections; 

public class MoveRacket : MonoBehaviour 
{ 
    // up and down keys (to be set in the Inspector) 
    public KeyCode up; 
    public KeyCode down; 

    void FixedUpdate() 
    { 
     // up key pressed? 
     if (Input.GetKey(up)) 
     { 
      transform.Translate(new Vector2(0.0f, 0.1f)); 
     } 

     // down key pressed? 
     if (Input.GetKey(down)) 
     { 
      transform.Translate(new Vector2(0.0f, -0.1f)); 
     } 
    } 
} 

이 시도 :

using UnityEngine; 
using System.Collections; 

public class MoveRacket : MonoBehaviour 
{ 
     public float speed = 30f; 
     //the speed at which you move at. Value can be changed if you want 

    void Update() 
    { 
     // up key pressed? 
     if (Input.GetKeyDown(KeyCode.W) 
     { 
      transform.Translate(Vector2.up * speed * time.deltaTime, Space.world); 
     } 

     // down key pressed? 
     if (Input.GetKeyDown(KeyCode.S)) 
     { 
      transform.Translate(Vector2.down * speed * time.deltaTime, Space.World); 
     } 
    } 
} 

당신이 운동을 위해 WASD 키를 사용하려면 가정. 원하는 경우 OR 수정 자 (||)를 사용하여 추가 할 수 있습니다. 또한 두 번째 플레이어의 경우 키 코드를 변경해야합니다. 그렇지 않으면 두 패들이 동시에 이동합니다.

코드 설명 : 속도 변수는 이동할 속도입니다. 필요에 따라 변경하십시오.

in transfor.Translate(), 일정한 속도로 시간 경과에 따라 세계 좌표 (로컬이 아님)로 이동하려고합니다. 그래서 Vector2.up * speed * time.deltaTime을 사용합니다. Vector2.up은이 프레임에서 이동하는 거리를 얻기 위해, 다음 Time.deltaTime에 의해 이동 거리를 얻기 위해 속도에 의해 곱

new Vector2 (0f, 1f); 

과 동일합니다. Update는 매 프레임마다 호출되므로 매 프레임마다 거리를 이동합니다. 아래로 이동에서

는 Vector2.down는

new Vector2(0f, -1f); 

희망이 도움과 동일합니다!

관련 문제