Shooting a ball (With gravity)

1.1k Views Asked by At

I am currently having trouble shooting a ball and adding velocity to it. The idea is that the longer you hold down "SPACE" the longer the ball will travel.

image

What I have so far is this for the player control:

public class PlayerControl : MonoBehaviour {

    public float speed = 0f;
    Vector3 enVector = new Vector3(10,0,0);
    public bool laserDirection = false;

    public Transform firePoint;
    public GameObject RedBall;

    public PlayerControl player;

    // Use this for initialization
    void Start () { }

    // Update is called once per frame
    void Update () 
    {

        // Press CTRL to move the platform under the ball and shoot a laser (NOT FINISHED)
        if (Input.GetKey (KeyCode.LeftControl) && laserDirection == false) 
        {
            transform.Translate(enVector * -speed * Time.deltaTime);
        } 
        else if(Input.GetKey (KeyCode.LeftControl) && laserDirection == true) 
        {

            transform.Translate(enVector * speed * Time.deltaTime);
        }

        // Sets the direction the platform will travel if pressed
        if(Input.GetKey (KeyCode.LeftArrow))
        {
            laserDirection = false;
        }

        // Sets the direction the platform will travel if pressed
        if(Input.GetKey (KeyCode.RightArrow))
        {
            laserDirection = true;
        }

        // Shoots a ball the longer you hold down
        if(Input.GetKey (KeyCode.Space))
        {
            GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed, GetComponent<Rigidbody2D> ().velocity.y);
            Instantiate(RedBall, firePoint.position, firePoint.rotation);
        }
    }

    // Destroy the ball
    void OnTriggerEnter2D(Collider2D other) 
    {
        Destroy (gameObject);
    }
}

If you look where the get space button is you see the code I've made for the shooting. This only makes the ball travel to the right at a set speed I've chosen.

This is for changing the direction of the ball (It moves around the big black ball):

// THE DIRECTION OF THE BALL SCRIPT
public class RedBall : MonoBehaviour 
{

    public Transform target;

    void Update() 
    {
        // Moves the ball launcher  to the left
        if (Input.GetKey (KeyCode.LeftArrow)) 
        {
            transform.RotateAround (target.position, transform.forward, Time.deltaTime * 90f);
        }

        // Moves the ball launcher  to the right
        if (Input.GetKey (KeyCode.RightArrow)) 
        {
            transform.RotateAround (target.position, -transform.forward, Time.deltaTime * 90f);
        }
    }
}

I am struggling with how to figure out how to make the ball shoot in the direction it is facing, which depends on how it has been translated in the ball script. Additionally, how to make the ball react to the gravity when shot and how to shoot further the longer I hold hold the "SPACE" button.

If anyone knows how to do at least one of these things it would help a lot! Thank you.

2

There are 2 best solutions below

5
On BEST ANSWER

So I am struggling with how to figure out how to make the ball shoot in the direction it is facing depending on how it have been translated in the ball script.

Multiply speed by the direction you want to shoot in:

// Shoots a ball the longer you hold down
    if(Input.GetKey (KeyCode.Space)) {

        GetComponent<Rigidbody2D> ().AddForce(transform.forward * speed);
        Instantiate(RedBall, firePoint.position, firePoint.rotation);
    }

Also look into object pooling, you really shouldnt be instantiating projectiles it takes alot of memory to instantiate and destroy all the time during execution.

Also how to make the ball have the gravity when shot

protected float gravity = 1f;
protected bool isShot = false;

// Update is called once per frame
void Update ()
{
    if(isShot)
        rigidBody.velocity.z += gravity * Time.deltaTime;
}

shoot longer the longer I hold hold the "SPACE" button.

public float rate = 1.0f;
protected float power = 0f;

// Update is called once per frame
void Update ()
{
    if (Input.GetKeyUp (KeyCode.Space))
    {
        UsePower(power);
        power = 0f;
    }
    if (Input.GetKey (KeyCode.Space))
    {
        power += rate * Time.deltaTime;
    }
}

void UsePower (float _power)
{
    // Use power here
}
7
On

(Instead of using Rigidbody2D.velocity try to use Rigidbody2D.AddForce. For turning gravity on and off use Rigidbody2D-gravityScale.

I'm not quite sure what you mean with "the longer I hold the "SPACE button". Do you want to make it a "Spring" and release it when the Space button was released?

edit: maybe do it like this. make initial force a variable and "Load it" while the button is pressed, when it is release, instantiate the Ball and add the force to it.

i justed typed this out of the head, without testing so maybe it wont run out of the box, but the direction should be clear

        if (Input.GetKey(KeyCode.Space))
        {
            initialForce += 0.1f;
            GetComponent<Rigidbody2D>().velocity = new Vector2(speed, GetComponent<Rigidbody2D>().velocity.y);
            Instantiate(RedBall, firePoint.position, firePoint.rotation);


        }
        else
        {
            if (initialForce > 0)
            {
                var ball = (GameObject)Instantiate(RedBall, firePoint.position, firePoint.rotation);
                ball.GetComponent<Rigidbody2D>.AddForce(firePoint.rotation * Vector2.one * initialForce);

            }
            initialForce = 0f;
        }