2016-07-26 4 views
0

유니티 엔니의 플레이어 컨트롤 스크립트 (C#)에 약간 문제가 있습니다. 나는 플레이어의 기본 동작으로 다음 스크립트를 작성했다. 문제는 플레이어가 jump 문 (디버그 로그에 출력 됨)을 입력 할 수 있다는 것입니다. Debug Log 하지만 작동하지 않습니다. 캐릭터는 아직 땅에있다. 점프 기능은 플레이어가 지상에있을 때 (접지) 두 번 점프하지 않았을 때 활성화됩니다. 내 질문에 거기에 어떤 "코드 실수"또는 내가 볼 수없는 구성 문제가 있습니까?캐릭터가 Unity2D에서 점프하지 않지만 점프 문을 입력했습니다.

미리 도움을 주셔서 감사합니다.

using UnityEngine; 
using System.Collections; 
public class PlayerControl : MonoBehaviour 
{ 

    // public variables 
    public float speed = 3f; 
    public float jumpHeight = 5f; 

    // private variables 
    Vector3 movement; 
    Animator anim; 
    Rigidbody2D playerRigidbody; 

    // variables for the ground check 
    public Transform groundCheck; 
    public float groundCheckRadius; 
    public LayerMask whatIsGround; 
    private bool grounded; 
    private bool doubleJump; 

    // Use this for initialization 
    void Start() 
    { 

    } 

    // Update is called once per frame 
    void Update() 
    { 
     // Proves if the player is on the ground and activate the double jump function 
     if (grounded) 
     { 
      doubleJump = false; 
     } 

     // First line of proving the jump 
     if (Input.GetMouseButtonDown(0) && grounded) 
     { 
      Debug.Log("Jump if entered"); 
      Jump(); 
     } 

     if (Input.GetMouseButtonDown(0) && !doubleJump && !grounded) 
     { 
      Debug.Log("double Jump"); 
      Jump(); 
      doubleJump = true; 
     } 


     // Flipping the Player when he runs back 
     if (Input.GetAxis("Horizontal") < 0) 
     { 
      playerRigidbody.transform.localScale = new Vector2(-1.7f, 1.7f); 
     } 

     else 
     { 
      playerRigidbody.transform.localScale = new Vector2(1.7f, 1.7f); 
     } 
    } 

    void Awake() 
    { 
     // References setting up 
     playerRigidbody = this.GetComponent<Rigidbody2D>(); 
     anim = GetComponent<Animator>(); 
    } 

    void FixedUpdate() 
    { 
     float horizontal = Input.GetAxisRaw("Horizontal"); 
     float vertical = Input.GetAxisRaw("Vertical"); 

     // simple Movement without a speed control 
     Move(horizontal, vertical); 
     Animating(horizontal, vertical); 

     // Section for ground detection 
     grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround); 

     // Set the parameter for the jump animation false or true 
     anim.SetBool("Grounded", grounded); 
    } 

    void Move(float horizontal, float vertical) 
    { 
     movement.Set(horizontal, 0f, vertical); 
     movement = movement.normalized * speed * Time.deltaTime; 

     playerRigidbody.MovePosition(transform.position + movement); 
    } 

    void Jump() 
    { 
     playerRigidbody.AddForce(Vector3.up * jumpHeight); 
     // playerRigidbody.AddForce(new Vector2(0f, jumpHeight), ForceMode2D.Impulse); 
     Debug.Log("Jump function"); 
    } 

    void Animating(float h, float v) 
    { 
     bool walking = h != 0f || v != 0f; 
     anim.SetBool("IsWalking", walking); 
    } 
} 
+0

점프의 애니메이션이 활성화되어 있습니까? –

+0

의견을 보내 주셔서 감사합니다. 문제는 애니메이션이 아니므로 캐릭터가 위치를 변경하지 않았습니다 (점프). 애니메이션이 작동하지 않을 때 그것을 처리 할 수 ​​있지만 위치를 변경하지 않는 문제는 아닙니다. 하지만 또 다른 이상한 점이 있습니다. 이 작은 코드를'void update()'함수에 넣습니다 : if (Input.GetKeyDown (KeyCode.Space)) playerRigidbody.AddForce (Vector2.up * jumpHeight); 그리고 이것도 작동하지 않습니다. – t3chnico

+0

공간이있는 다른 애니메이션 동작을 넣으면 작동합니까? 또한, 나는 주인공 객체에서 3 인칭 컨트롤러를 사용하고 공간과 함께 점프가 준비되어 있음을 기억합니다. –

답변

0

여기서 추측 하겠지만 2D 물리학에서는 Vector3.up이 작동하지 않을 수 있습니다. 2D가 아니기 때문에 시도해 볼 수 있습니다.

playerRigidbody.AddForce(transform.up * jumpHeight); 

대신에.

또한 jumpHeight에 다른 값을 사용해 보셨습니까? 5 당신이 당신의 강체를 위해 세운 질량에 따라 작을 수도 있습니다. 그리고 검사기에서 축을 제한하지 않았는지 확인하십시오.

+0

OMG! @ 타로크 당신이 내 눈을 뜨게! 어제는 jumpHeigt의 다른 설정을 시도했습니다. 모두 재생 모드입니다. 큰 힌트를 가져 주셔서 감사합니다. – t3chnico

+0

@ t3chnico 기꺼이 도와 드릴 수 있습니다.) – Tarrokk

0
// Note: If you want the object to move in a reliable predictable way but still allow physics interactions, use MovePosition (set the object to kinematic if you want it to be unaffected by physics but still be able to affect other things, and uncheck kinematic if you want both objects to be able to be acted on by physics. 

If you want to move your object but let physics handle the finer details, add a force. 

playerRigidbody.rigidbody2D.AddForce(Vector3.up * 10 * Time.deltaTime); 

use Vector.up, Vector.down, vector.right, Vectore.left along with time.deltaTime to have smooth movement along the frame.