유니티 엔니의 플레이어 컨트롤 스크립트 (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);
}
}
점프의 애니메이션이 활성화되어 있습니까? –
의견을 보내 주셔서 감사합니다. 문제는 애니메이션이 아니므로 캐릭터가 위치를 변경하지 않았습니다 (점프). 애니메이션이 작동하지 않을 때 그것을 처리 할 수 있지만 위치를 변경하지 않는 문제는 아닙니다. 하지만 또 다른 이상한 점이 있습니다. 이 작은 코드를'void update()'함수에 넣습니다 : if (Input.GetKeyDown (KeyCode.Space)) playerRigidbody.AddForce (Vector2.up * jumpHeight); 그리고 이것도 작동하지 않습니다. – t3chnico
공간이있는 다른 애니메이션 동작을 넣으면 작동합니까? 또한, 나는 주인공 객체에서 3 인칭 컨트롤러를 사용하고 공간과 함께 점프가 준비되어 있음을 기억합니다. –