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

Time:05-18

I have a problem with writing a function that throws spawned ball by pressing a button. So far, i could only instantiate the ball and it is affected by gravity. I can not wrap my head arround, how in my code should i add a force to push it with some force. Right now it just plops in front of a player.

I tried to fiddle with .AddForce() on a ball rigidbody2D and still nothing.

I would greatly appreciate if someone could take a look at the code and guide me into some directcion on where to AddForce for ball to make it move?

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;
        }
    }



}

CodePudding user response:

You might need a variable to check if the player is facing right/left. If the player is, just add force to the ball depending on the direction.

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;

CodePudding user response:

So, i manage to fiddle with the .AddForce() method and i came up with a solution to my problem.

My OnFire script looks like this:

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

No change here. I made another script called "BallBehaviour.cs" and added it to the ball with a code like this:

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

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

    void Start() 
    {
        player = FindObjectOfType<PlayerControl>();
        xSpeed = player.transform.localScale.x * ballForce;
        Throw();    
    }


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

Basically, the method Throw() tells the ball to check the direction the player is facing and then multiply it by ballForce. Then, it uses the ForceMode2d.Impulse method to apply calculated force to the ball for a second and it works on left or right direction.

Thanks Jkimishere for a nudge in a direction!

  • Related