我的統一對象中的播放器控制腳本(C#)有點問題。我根據玩家的基本動作制定了以下腳本。問題是播放器可以輸入跳轉語句(調試日誌打印出來) 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
如果你把另一個動畫運動與空間,它的作品? 另外,據我所知,在我的主要角色對象中,我使用了第三人稱控制器,並且跳躍空間本身已經準備就緒 –