如何扔一个球员在Unity 2d项目中捡起的球

How to throw a ball that player picked up in Unity 2d project

我在编写一个通过按下按钮抛出生成的球的函数时遇到了问题。 到目前为止,我只能实例化球,它受重力影响。 我无法绕过我的头,我应该如何在我的代码中添加一个力来用一些力推动它。现在它只是扑通一声出现在玩家面前。

我尝试 fiddle 在球 rigidbody2D 上使用 .AddForce() 但仍然没有。

如果有人能看一下代码并指导我在何处添加力量使球移动,我将不胜感激?

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

public class PlayerControl : MonoBehaviour
{
    [Header("Player Atributes")]
    [SerializeField] float runSpeed = 10f;
    Vector2 moveInput;
    Rigidbody2D playerRigidbody2D;

    [Header("Ball Settings")]
    public bool playerHasABall = false;
    [SerializeField] GameObject ball;
    [SerializeField] Transform ballSpawn;


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

    void Update()
    {
        Run();
        FlipSprite();
    }

    void OnMove(InputValue value)
    {
        moveInput = value.Get<Vector2>();
    }
        
    void OnFire(InputValue value)
    {

        if(!playerHasABall) {return;}
        Instantiate(ball, ballSpawn.position, transform.rotation);
        playerHasABall = false;
    }

    void Run()
    {   
        Vector2 playerVelocity = new Vector2 (moveInput.x * runSpeed, playerRigidbody2D.velocity.y);
        playerRigidbody2D.velocity = playerVelocity;
    }

    void FlipSprite()
    {
        bool playerHasHorizontalSpeed = Mathf.Abs(playerRigidbody2D.velocity.x) > Mathf.Epsilon;
        
        if (playerHasHorizontalSpeed)
        {
            transform.localScale = new Vector2 (Mathf.Sign(playerRigidbody2D.velocity.x), 1f);
        }   
    }

    //Ball picking up script. It destroys the ball with a tag "Ball" and sets a bool playerHasABall to true
    private void OnTriggerEnter2D(Collider2D other) 
    {
        if(other.tag == "Ball")
        {
            Destroy(other.gameObject);
            playerHasABall = true;
        }
    }



}

您可能需要一个变量来检查玩家是否面向 right/left。 如果玩家是,则根据方向对球施加力即可。

if(!playerHasABall) {return;} 
  Instantiate(ball, 
  ballSpawn.position, 
     transform.rotation);
     //Check which way the player is facing and add forrce to that direction.
     //Adding a FacingRight boolean might help..... 
     
     playerHasABall = false;

所以,我设法 fiddle 再次使用 .AddForce() 方法,我想出了解决问题的办法。

我的 OnFire 脚本如下所示:

    void OnFire(InputValue value)
    {
        if(!playerHasABall) {return;}
        Instantiate(ball, ballSpawn.position, transform.rotation);
        playerHasABall = false;
    }

这里没有变化。我制作了另一个名为“BallBehaviour.cs”的脚本,并使用如下代码将其添加到球中:

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

public class BallBehaviour : MonoBehaviour
{
    [SerializeField] float ballForce = 20f;
    PlayerControl player;

    void Start() 
    {
        player = FindObjectOfType<PlayerControl>();
        Throw();    
    }


    public void Throw()
    {
        GetComponent<Rigidbody2D>().AddForce(player.transform.localScale * ballForce, ForceMode2D.Impulse);
    }
}

基本上,方法 Throw() 告诉球检查玩家面对的方向,然后将其乘以 ballForce。然后,它使用 ForceMode2d.Impulse 方法将计算好的力施加到球上一秒钟,并在向左或向右方向上起作用。

感谢 Jkimishere 在正确方向上的推动!