Skip to content
Snippets Groups Projects
GameManager.cs 1.26 KiB
Newer Older
william's avatar
william committed
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
william's avatar
william committed
    private Animator stateMachine;

    [SerializeField] private GameObject ball;
    [SerializeField] private Transform target;
william's avatar
william committed
    [SerializeField] private Transform throwPoint;
    [SerializeField] private GameObject player;
william's avatar
william committed

    public void shoot(float force)
    {
        float xdistance;
        float time = 0.8f + force;
        xdistance = target.position.x - throwPoint.position.x;
william's avatar
william committed
        float ydistance;
        ydistance = target.position.y - throwPoint.position.y;
william's avatar
william committed

        float throwAngle; 
        throwAngle = Mathf.Atan((ydistance + calculateGravity() * (time * time)) / xdistance);
        float totalVelo = xdistance / (Mathf.Cos(throwAngle) * time);

        float xVelo, yVelo;
        xVelo = totalVelo * Mathf.Cos(throwAngle);
        yVelo = totalVelo * Mathf.Sin(throwAngle);

        GameObject bulletInstance = Instantiate(ball, throwPoint.position, throwPoint.rotation) as GameObject;
william's avatar
william committed
        bulletInstance.GetComponent<Rigidbody2D>().velocity = new Vector2(xVelo, yVelo);
        player.GetComponent<PlayerController>().changeTurn();
william's avatar
william committed
    }

    private float calculateGravity()
    {
        return Physics2D.gravity.y / -2;
    }
}