试图做一个壁架。问题:transform.position 总是返回vector(0, 0)

Posted

技术标签:

【中文标题】试图做一个壁架。问题:transform.position 总是返回vector(0, 0)【英文标题】:Trying to making a ledge. The problem: transform.position always returns to vector( 0, 0) 【发布时间】:2021-05-17 23:03:44 【问题描述】:

我正在尝试制作一种可以让玩家攀爬壁架的方法。我有两个光线投射,一个用于 Ledge,一个用于墙壁检测。我的窗台是假的,然后爬上窗台。 问题来了: 当 Wall check 为 true 并且 ledge Check 为 true 时,将 player transform.position 设置为靠近 ledge。然后将 transform.position 置于壁架之上。但由于某种原因,每当我尝试在壁架附近调用 Set player transform.position 时,它只会传送回默认向量值 0、0。

    [Header("Ledge check")]
    private bool canClimbLedge = false;
    private bool ledgeDetected;

    private Vector2 workspace;

    //NEW SHIT
    private Vector2 detectedPos;
    private Vector2 cornerPos;
    private Vector2 startPos;
    private Vector2 stopPos;
    [Header("Ledge Climb State")]
    public Vector2 startOffset;
    public Vector2 stopOffset;

private void FixedUpdate()
        if (coll.isTouchingWall && !coll.isTouchingRightLedge || !coll.isTouchingLeftLedge)
        
            SetDetectedPosition(transform.position);
        


void Update()
    
        DetermineCornerPosition();

        if (coll.onWall && !coll.isTouchingRightLedge)
        CheckLedgeClimb();
        if(canClimbLedge && coll.onGround)
        FinishLedgeClimb();

        if (dir.x < 0)
        
            if (groundTouch == true && !wallGrab)
               // dustParticle.Play();

            if (wallGrab != true)
            
                side = -1;
                //anim.Flip(side);

            

        

        if (dir.x > 0)
        
            if (groundTouch == true && !wallGrab)
                // dustParticle.Play();

                if (wallGrab != true)
                
                    side = 1;
                    // anim.Flip(side);
                
        
    

public void SetDetectedPosition(Vector2 pos) => detectedPos = pos;
    public Vector2 DetermineCornerPosition()
    
        RaycastHit2D xHit = Physics2D.Raycast(coll.wallCheck.position, Vector2.right * side, coll.wallCheckDistance, coll.groundLayer);
        float xDist = xHit.distance;
        workspace.Set((xDist * side) * side, 0.015f);
        RaycastHit2D yHit = Physics2D.Raycast(coll.ledgeCheck.position + (Vector3)(workspace), Vector2.down, coll.ledgeCheck.position.y - coll.wallCheck.position.y + 0.015f, coll.groundLayer);
        float yDist = yHit.distance;

        //Upper Corner Position of ledge
        workspace.Set(coll.wallCheck.position.x + (xDist * side), coll.ledgeCheck.position.y - yDist);
        return workspace;
    
    private void CheckLedgeClimb()
    

        if (coll.isTouchingWall && !coll.isTouchingRightLedge && !ledgeDetected)
        
            ledgeDetected = true;
            //Freeze player in the detectedPos
            rb.velocity = Vector2.zero;
            rb.gravityScale = 0f;
            transform.position = detectedPos;
            cornerPos = DetermineCornerPosition();
        

        if(ledgeDetected && !canClimbLedge)
        
            canClimbLedge = true;
            startPos.Set(cornerPos.x - (side * startOffset.x), cornerPos.y - startOffset.y);
            stopPos.Set(cornerPos.x + (side * stopOffset.x), cornerPos.y + stopOffset.y);
        

        Debug.Log(startPos);
        canMove = false;
        transform.position = startPos;
        canClimbLedge = true;
    

    public void FinishLedgeClimb()
    
     //Call the last part of climbing the ledge
    

在碰撞脚本中:

void FixedUpdate()

    isTouchingWall = Physics2D.Raycast(wallCheck.position, transform.right, wallCheckDistance, groundLayer);

    isTouchingRightLedge = isTouchingWall = Physics2D.Raycast(ledgeCheck.position, transform.right, wallCheckDistance, groundLayer);

任何帮助都将不胜感激,如果您有任何其他解决方案来制作壁架攀爬器,我会全力以赴。

【问题讨论】:

CheckLedgeClimb() 你总是设置transform.position = startPos; 这可能是问题吗? 【参考方案1】:

我重新评估了脚本的目的。 问题是 Raycast 并没有真正正常工作 如果玩家面朝左,我没有让光线投射向一个方向发射,而是让它跟随变化 180 度。

在我的游戏中,我不希望玩家能够悬挂在壁架上,因此删除了 transform.position。 变化:

碰撞脚本:

 isTouchingWall = Physics2D.Raycast(wallCheck.position, transform.right * move.side, wallCheckDistance, groundLayer);

        isTouchingLedge = Physics2D.Raycast(ledgeCheck.position, transform.right * move.side, wallCheckDistance, groundLayer);

ClimbLedge 功能:

 private void CheckLedgeClimb()
    

        if (coll.isTouchingWall && !coll.isTouchingLedge && !ledgeDetected)
        
            //Reference point to startPos and stopPos
            ledgeDetected = true;
            //Freeze player in the detectedPos
            rb.velocity = Vector2.zero;
            rb.gravityScale = 0f;
            transform.position = detectedPos;
            cornerPos = DetermineCornerPosition();
            Debug.Log("cornerPos Vector Value: " + cornerPos);
            canClimbLedge = false;
        

        if(ledgeDetected && !canClimbLedge)
        

            //This part is only necessary if you want the player to hang from a ledge.
            /*startPos.Set(cornerPos.x - (side * startOffset.x), cornerPos.y - startOffset.y);
            stopPos.Set(cornerPos.x + (side * stopOffset.x), cornerPos.y + stopOffset.y);

            Debug.Log("startPos when ledgeDetected + !canClimbLedge:" + startPos);
            canMove = false;
            transform.position = startPos;
            rb.velocity = Vector2.zero;
            rb.gravityScale = 0f;*/

            stopPos.Set(cornerPos.x + (side * stopOffset.x), cornerPos.y + stopOffset.y);
            rb.velocity = Vector2.zero;
            rb.gravityScale = 0f;
            canMove = false;
            canClimbLedge = true;
        
    

    public void FinishLedgeClimb()
    
       if (canClimbLedge)
        
            transform.position = stopPos;
            canClimbLedge = false;
            canMove = true;
            ledgeDetected = false;
            Debug.Log("transform.position = stopPos");

            //anim.SetBool("canClimbLedge", canClimbLedge);
         
    

这是可行的,但玩家将 transform.position 转换为 stopPos 的时间有时会延迟。

【讨论】:

以上是关于试图做一个壁架。问题:transform.position 总是返回vector(0, 0)的主要内容,如果未能解决你的问题,请参考以下文章

如何找到 SKSpriteNode 的速度作为 x 和 y 值?

将玩家绑定到路径 Unity

试图做一个复合谓词。不工作。

试图做一个二十一点游戏

试图做一个短路 && 以及将错误分配给一个字符串?

jQuery & Objects,试图做一个轻量级的小部件