【问题标题】:Unity2D: Restricting player controlled characters from moving each otherUnity2D:限制玩家控制的角色相互移动
【发布时间】:2019-01-27 05:40:59
【问题描述】:

我目前正在制作风格类似于蠕虫游戏的 2D 巡回平台游戏。我想在我的游戏中重现一件事,那就是团队成员之间的互动方式。

我正在寻找一种解决方案来锁定角色之间的速度转移。假设当前处于玩家控制之下的一个角色正在向另一个处于空闲状态的角色移动。当这些角色发生碰撞时,可控的角色应该无法进一步前进。但是,当第一个仍在移动时,它开始移动第二个。

所以我正在寻找一种解决方案来防止当前控制角色的速度转移到其他角色的情况。

下面是一个负责移动角色的 PlayerController 脚本:

private float MoveSpeed = 5f;
private float JumpSpeed = 15f;
private float MoveInput;

private Rigidbody2D rb;

public bool FacingRight = true;
public bool Grounded;
private bool Jump;

public Transform GroundCheck;
public Transform HealthTag;
public LayerMask Ground;

public Animator animator;

private void Start()
{
    rb = GetComponent<Rigidbody2D>();
}

private void Update()
{
    if (Input.GetButtonDown("Jump") && Grounded == true)
    {
        Jump = true;

        if (Jump == true)
        {
            rb.velocity = new Vector2(rb.velocity.x, JumpSpeed);
        }       
    }
}

private void FixedUpdate()
{
    Grounded = Physics2D.OverlapCircle(GroundCheck.position, 0.5f, Ground);

    if(Grounded == false)
    {
        Jump = false;
    }

    MoveInput = Input.GetAxis("Horizontal");

    animator.SetFloat("Speed", Mathf.Abs(MoveInput));

    rb.velocity = new Vector2(MoveInput * MoveSpeed, rb.velocity.y);

    if (MoveInput > 0 && FacingRight == false)
    {
        Flip();
    }

    else if (MoveInput < 0 && FacingRight == true)
    {
        Flip();
    }        
}

void Flip()
{
    FacingRight = !FacingRight;

    if (FacingRight == true)
    {
        transform.eulerAngles = new Vector3(0, 0, 0);
        HealthTag.transform.eulerAngles = new Vector3(0, 0, 0);

    }
    if (FacingRight == false)
    {
        transform.eulerAngles = new Vector3(0, 180, 0);
        HealthTag.transform.eulerAngles = new Vector3(0, 0, 0);
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您必须将闲置玩家的刚体设置为运动学。这将防止它被移动的玩家推动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多