跳转/投掷编码或动画问题

问题描述 投票:0回答:1

设置

我有一个带动画师的篮球运动员,并为动画师分配了5个动画,如下所示:

  1. PlayerIdle
  2. PlayerWalk
  3. PlayerJump
  4. PlayerBend
  5. PlayerShoot

Setup_FruitHoops

Player Animation Window with Parameters

问题

在PlayMode中,播放器能够成功移动和跳跃。当玩家尝试抓住地面上的苹果时,就会出现问题。我想要的理想游戏设计是让玩家弯腰抓住苹果,方法是按G键,然后再次按G键,将苹果向篮筐扔一分。但是,当我运行它并首次按下G后,玩家将无法再跳,抓或扔。下一节是PlayerMovement和GrabberScript的代码。任何建议都将不胜感激。

PlayerMovement.cs

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

public class PlayerMovement : MonoBehaviour
{
    public float speed = 5f;
    private Rigidbody2D rb;
    private Animator anim;
    private bool isGrounded, jumped;
    private float jumpshotPower = 6f;

    public Transform groundCheck;
    public LayerMask groundLayer;

    private void Awake()
    {
        rb = GameObject.Find("Player Parent").GetComponent<Rigidbody2D>();
        anim = GameObject.Find("Player Parent").GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        CheckIfGrounded();
        PlayerJump();
    }

    private void FixedUpdate()
    {
        PlayerWalk();
    }

    void PlayerWalk()
    {
        float h = Input.GetAxisRaw("Horizontal");

        if (h > 0)
        {
            //going right
            anim.SetBool("isWalking", true);
            rb.velocity = new Vector2(speed, rb.velocity.y);
            ChangeDirection(-1);
        }
        else if (h < 0)
        {
            //going left
            anim.SetBool("isWalking", true);
            rb.velocity = new Vector2(-speed, rb.velocity.y);
            ChangeDirection(1);
        }
        else
        {
            //standing still
            anim.SetBool("isWalking", false);
            rb.velocity = new Vector2(0f, rb.velocity.y);
        }
    }

    void ChangeDirection(int d)
    {
        Vector3 temp = transform.localScale;
        temp.x = d;
        transform.localScale = temp;
    }

    void CheckIfGrounded()
    {
        isGrounded = Physics2D.Raycast(groundCheck.position, Vector2.down, 0.1f, groundLayer);
        if (isGrounded)
        {
            if (jumped)
            {
                jumped = false;
                anim.SetBool("isJumping", false);
            }
        }
    }
    void PlayerJump()
    {
        if (isGrounded)
        {
            if (Input.GetKey(KeyCode.UpArrow))
            {
                jumped = true;
                rb.velocity = new Vector2(rb.velocity.x, jumpshotPower);
                anim.SetBool("isJumping", true);
            }
        }
    }
}

GrabberScript.cs

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

public class GrabberScript : MonoBehaviour
{
    private bool grabbed, throwObject;
    private RaycastHit2D hit;
    public float distance;
    public Transform holdpoint;
    public float throwForce;
    public LayerMask notFruitLayer;
    private Animator anim;

    private void Awake()
    {
        anim = GameObject.Find("Player Parent").GetComponent<Animator>();
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.G))
        {
            if (!grabbed)
            {
                //grab fruit
                anim.Play("PlayerBend");
                Physics2D.queriesStartInColliders = false;
                hit = Physics2D.Raycast(transform.position, Vector3.down * transform.localScale.x, distance);

                if(hit.collider != null && hit.collider.tag == "Apple")
                {
                    grabbed = true;
                }
            }
            else if(!Physics2D.OverlapPoint(holdpoint.position, notFruitLayer))
            {
                //throw fruit
                grabbed = false;

                if(hit.collider.gameObject.GetComponent<Rigidbody2D>() != null)
                {
                    PlayerShooting();
                }
            }

            if (grabbed)
            {
                hit.collider.gameObject.transform.position = holdpoint.position;
            }
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.green;
        Gizmos.DrawLine(transform.position, transform.position + Vector3.down * transform.localScale.x * distance);
    }

    void PlayerShooting()
    {
        if (grabbed == true)
        {
            if (Input.GetKey(KeyCode.G))
            {
                throwObject = true;
                hit.collider.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(transform.localScale.x, 1) * throwForce;
                anim.Play("PlayerShoot");
            }
        }
    }
}
c# unity3d animation unity3d-2dtools
1个回答
0
投票

我们可以看到您的动画师状态吗?我认为没有办法从动画师的“ PlayerBend”状态退回。在您的步行代码中,您正在更改“ isWalking”布尔值,但是如果您处于正确的状态,它将起作用。所以我的建议是开始游戏并仔细检查动画师的状态。

© www.soinside.com 2019 - 2024. All rights reserved.