我是 C# 新手,我正在制作一個游戲來學習。誰能幫幫我,我在網上為我的播放器找到了一些腳本。我嘗試為它制作影片并且我使用了“Bool”,但有時當玩家開始行走時它并沒有影片。我能做些什么?我添加了翻轉來左右翻轉播放器。我在 SetBool 中添加了“If”,用于從“Idle”到“IsWalking”的轉換。 我的影片師的截圖
我的播放器有 4 個腳本。其他 3 個在以下鏈接中:[鏈接](https://drive.google.com/drive/folders/1F_zbQJgihv82zjg5pcQ9L_dp0sgA2O2Q?usp=sharing)
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Player))]
public class PlayerInput : MonoBehaviour {
private Animator anim;
private bool m_FacingRight = true;
void Start () {
player = GetComponent<Player> ();
anim = GetComponent<Animator>();
}
void Update () {
Vector2 directionalInput = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
player.SetDirectionalInput (directionalInput);
if (Input.GetAxisRaw("Horizontal") > 0 && !m_FacingRight)
{
Flip();
anim.SetBool("IsWalking", true);
}
if (Input.GetAxisRaw("Horizontal") < 0 && m_FacingRight)
{
Flip();
anim.SetBool("IsWalking", true);
}
if (Input.GetAxisRaw("Horizontal") == 0 && !m_FacingRight)
{
anim.SetBool("IsWalking", false);
}
if (Input.GetAxisRaw("Horizontal") == 0 && m_FacingRight)
{
anim.SetBool("IsWalking", false);
}
if (Input.GetKeyDown (KeyCode.Space)) {
player.OnJumpInputDown ();
anim.SetBool("IsJumping", true);
}
if (Input.GetKeyUp (KeyCode.Space)) {
player.OnJumpInputUp ();
anim.SetBool("IsJumping", false);
}
}
private void Flip()
{
// Switch the way the player is labelled as facing.
m_FacingRight = !m_FacingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}}
uj5u.com熱心網友回復:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerInput : MonoBehaviour
{
private Animator anim;
private bool _right ;
private bool _left ;
public float MoveSpeed;
void Start()
{
anim = GetComponent<Animator>();
player = GetComponent<Player> ();
}
void Update()
{
Vector2 directionalInput = new Vector2(Input.GetAxisRaw("Horizontal")*Time.deltaTime, Input.GetAxisRaw("Vertical") * Time.deltaTime);
player.SetDirectionalInput (directionalInput);
if (Input.GetAxisRaw("Horizontal") > 0)
{
TurnRight();
anim.SetBool("IsWalking", true);
}
if (Input.GetAxisRaw("Horizontal") < 0)
{
TurnLeft();
anim.SetBool("IsWalking", true);
}
if (Input.GetAxisRaw("Horizontal") == 0)
{
anim.SetBool("IsWalking", false);
}
if (Input.GetKeyDown (KeyCode.Space)) {
player.OnJumpInputDown ();
anim.SetBool("IsJumping", true);
}
if (Input.GetKeyUp (KeyCode.Space)) {
player.OnJumpInputUp ();
anim.SetBool("IsJumping", false);
}
}
public void TurnRight()
{
if (_right) return;
transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, 0);
_right = true;
_left = false;
}
public void TurnLeft()
{
if (_left) return;
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, 0);
_left = true;
_right = false;
}
}
你的腳本有一些問題。例如,一旦我開始向右移動,m_FacingRight 將為真,然后我停止移動。盡管如此,m_FacingRight 是正確的。由于 m_FacingRight 仍然為 true,如果我再次開始向右移動,則不會播放 walk 的影片。因為你用過
if (Input.GetAxisRaw("Horizontal") > 0 && !m_FacingRight)
左側也一樣
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/506575.html
上一篇:C函式的雙下劃線指標運算子的用途