Unity 2D角色在碰撞时持续播放行走动画,射线投射不按预期工作。

huangapple go评论58阅读模式
英文:

Unity 2D character keeps playing walking animation when colliding and raycasts don't work as expected

问题

我一直在开发一个2D顶视角的角色扮演游戏,已经添加了行走动画等。我想要在玩家碰到墙壁时停止行走动画。目前,我使用了一个包围盒碰撞器和射线投射。射线投射最初在向下行走时会命中玩家的碰撞器,但使用图层掩码后,这个问题停止了。然而,当向左或向右行走时,出现了两个无法解决的问题。首先,当向上或向下行走进入位于碰撞层的瓦片地图时(这个瓦片地图有瓦片地图碰撞器,会阻止玩家穿过它们),动画仍然会播放。其次,当两个瓦片相互贴在一起时,玩家只会发生一次碰撞,而不是重复碰撞。以下是我的碰撞检测代码,用于处理碰撞,用于碰撞的瓦片位于第6层。

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

public class PlayerScript : MonoBehaviour
{
    public float moveSpeed;
    private Animator ani;
    private bool isMoving;
    private Vector2 lastMove;
    private Rigidbody2D body;
    private Vector2 movement;
    private LayerMask wallLayer = 1 << 6;

    void Start()
    {
        body = GetComponent<Rigidbody2D>();
        ani = GetComponent<Animator>();
        movement = Vector2.zero;
        isMoving = false;
    }

    void Update()
    {
        isMoving = false;
        movement = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

        RaycastHit2D ray = Physics2D.Raycast(body.position, movement, 0.6f, wallLayer);

        if ((movement.x != 0f || movement.y != 0f) && !(ray && ray.collider.tag == "wall"))
        {
            isMoving = true;
            lastMove = movement;
        }

        ani.SetFloat("MoveX", movement.x);
        ani.SetFloat("MoveY", movement.y);
        ani.SetFloat("LastX", lastMove.x);
        ani.SetFloat("LastY", lastMove.y);
        ani.SetBool("IsMoving", isMoving);
    }

    void FixedUpdate()
    {
        body.MovePosition(body.position + movement * moveSpeed * Time.deltaTime);
    }
}

这是你提供的代码的翻译。

英文:

I have been working on a 2d top down rpg game and I have added walking animations etc, I want to stop the player from doing a walking animation when they hit a wall and currently I have a box collider with a ray cast, the ray cast originally hit the player box collider when walking down but after using a layermask this has stopped, however while walking left and right work perfectly two issues occur that I cannot seem to fix. First, when walking up or down into a tilemap that is on the collision layer (this tilemap has tilemap collider which will stop the player from walking through them) the animation still plays, and second the player will only collide once instead of repeatedly when hitting the tilemap when two tiles are placed back to back, here is my code for collision, the tiles that are for collision are on layer 6.

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

public class PlayerScript : MonoBehaviour
{
    public float moveSpeed;
    private Animator ani;
    private bool isMoving;
    private Vector2 lastMove;
    private Rigidbody2D body;
    private Vector2 movement;
    private LayerMask wallLayer = 1 &lt;&lt; 6;
    // Start is called before the first frame update
    void Start()
    {
        body = GetComponent&lt;Rigidbody2D&gt;();
        ani = GetComponent&lt;Animator&gt;();
        movement = Vector2.zero;
        isMoving = false;

    }

    // Update is called once per frame
    void Update() {
        isMoving = false;
        movement = new Vector2(Input.GetAxisRaw(&quot;Horizontal&quot;), Input.GetAxisRaw(&quot;Vertical&quot;));

        RaycastHit2D ray = Physics2D.Raycast(body.position, movement, 0.6f, wallLayer);
    
        if((movement.x != 0f || movement.y != 0f) &amp;&amp; !(ray &amp;&amp; ray.collider.tag == &quot;wall&quot;)) {
            isMoving = true;
            lastMove = movement;
        }

        ani.SetFloat(&quot;MoveX&quot;, movement.x);
        ani.SetFloat(&quot;MoveY&quot;, movement.y);
        ani.SetFloat(&quot;LastX&quot;, lastMove.x);
        ani.SetFloat(&quot;LastY&quot;, lastMove.y);
        ani.SetBool(&quot;IsMoving&quot;, isMoving);
    } 

    void FixedUpdate() {
       body.MovePosition(body.position + movement * moveSpeed * Time.deltaTime);
    }
}

答案1

得分: 1

代码看起来没问题。很可能问题出在AnimatorController的配置上。

你是否已经设置了状态过渡,比如从行走动画返回到空闲状态?

英文:

The code looks fine. Most likely, the issue is with your configuration of the AnimatorController.

Do you have transitions in place for (for example) coming from the walk animation back to idle?

huangapple
  • 本文由 发表于 2023年2月6日 18:53:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360363.html
  • 2d
  • c#
  • unity-game-engine

为所有缺失的枚举添加到List中。 go 54
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定

  • 开发者交流平台

    本页二维码