Newer
Older
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
private Animator stateMachine;
[SerializeField] private GameObject ball;
[SerializeField] private Transform target;
[SerializeField] private GameObject player;
public void shoot(float force)
{
float xdistance;
float time = 0.8f + force;
xdistance = target.position.x - throwPoint.position.x;
ydistance = target.position.y - throwPoint.position.y;
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;
bulletInstance.GetComponent<Rigidbody2D>().velocity = new Vector2(xVelo, yVelo);
player.GetComponent<PlayerController>().changeTurn();