Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何更改玩家的默认攻击方向?_C#_Unity3d_2d Games - Fatal编程技术网

C# 如何更改玩家的默认攻击方向?

C# 如何更改玩家的默认攻击方向?,c#,unity3d,2d-games,C#,Unity3d,2d Games,我在我的平台游戏中对我的玩家进行了编码,这样当你按下空格键时,他就会用剑攻击。只要我跑对了,它就会攻击我。当我向左跑的时候它会攻击我的左边。但当我默认地站着不动时,它会攻击左边。我如何使它正确地攻击 下面是我的播放器控制器脚本中的所有代码,也是我的混合树的图像 using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBe

我在我的平台游戏中对我的玩家进行了编码,这样当你按下空格键时,他就会用剑攻击。只要我跑对了,它就会攻击我。当我向左跑的时候它会攻击我的左边。但当我默认地站着不动时,它会攻击左边。我如何使它正确地攻击

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

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);
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家控制器:单行为
{
公共交通速度;
私人动画师;
公共int-playerJumpPower=1250;
私人浮动moveX;
公共学校停课;
公共浮点数fJumpWaitTime=0.2f;
私人浮动等待;
私有对象;
私人布尔攻击;
公众浮标攻击时间;
专用浮点攻击计时器;
//在第一帧更新之前调用Start
void Start()
{

anim=GetComponent

在再次查看混合树之后,我会检查您的Threst是否存在问题

您现在拥有的:

左0-1

玩家右1 1

您应该拥有的内容:

player左-0.01-1

玩家右0 1


你在哪里播放右击还是左击?你添加的代码只播放攻击动画,不区分它们。我希望我站着的时候它是右击,而不是左击。问题是你只向左击,它似乎只向右击,因为你翻转了角色(至少我猜如果没有更多的代码,没有看到混合树,我很难说出这一点)。我发送了混合树的图片作为答案。您的代码看起来很好,除了有void
OnCollisionInter2d(){}之外,不应该成为问题
更新中的内容不应起作用。评论不用于扩展讨论;此对话已被删除。