如何更改播放器的默认攻击方向?

时间:2020-07-17 14:55:50

标签: c# unity3d 2d-games

我在平台游戏中为玩家编码,以便当您按空格键时他会用剑攻击。当我正确运行时,它会正确地攻击。当我向左走时,它向左攻击。但是,当我默认情况下静止不动时,它就会留下攻击。我该如何使它正确攻击?

下面是我的播放器控制器脚本中的所有代码,也是我的混合树的图像。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class PlayerController : MonoBehaviour
{
    public int moveSpeed;

    private Animator anim;

    public int playerJumpPower = 1250;
    private float moveX;
    public bool isGrounded;
    public float fJumpWaitTime = 0.2f;
    private float fJumpWait;
    private object col;
    private bool attacking;
    public float attackTime;
    private float attackTimeCounter;

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (!attacking)
        {
            if (Input.GetButtonDown("Jump"))
            {
                Jump();
            }
            if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
            {
                transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
            }
            void Jump()
            {
                //Jumping Code
                GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumpPower);
                isGrounded = false;
            }
            void OnCollisionEnter2D(Collision2D col)
            {
                Debug.Log("Player has collided with " + col.collider.name);
                if (col.gameObject.tag == "ground")
                {
                    isGrounded = true;
                }
            }
        }
        
        anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));

        if (Input.GetKeyDown(KeyCode.Space))
        {
            attackTimeCounter = attackTime;
            attacking = true;   
            anim.SetBool("Attack", true);    
        }
if(attackTimeCounter > 0)
        {
            attackTimeCounter -= Time.deltaTime;
        }

if(attackTimeCounter <= 0)
        {
            attacking = false;
            anim.SetBool("Attack", false);
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

再次查看您的混合树后,我将检查您的Threst是否是问题所在。

您现在拥有的内容:

PlayerAttackLeft 0 -1

PlayerAttackRight 1 1

您可能应该拥有的东西:

PlayerAttackLeft -0.01 -1

PlayerAttackRight 0 1

Image of what I mean