Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestScript : MonoBehaviour {
private Animator stateMachine;
[SerializeField] private GameObject ball;
[SerializeField] private GameObject target;
[SerializeField] private Transform throwPoint;
[SerializeField] private GameObject player;
private Animator playerAnimator;
private void Start()
{
playerAnimator = player.GetComponent<Animator>();
}
// Update is called once per frame
public void shoot(float force)
{
float xdistance;
float time = 0.8f + force;
xdistance = target.transform.position.x - throwPoint.transform.position.x;
float ydistance;
ydistance = target.transform.position.y - throwPoint.transform.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.transform.position, throwPoint.rotation) as GameObject;
bulletInstance.GetComponent<Rigidbody2D>().velocity = new Vector2(xVelo, yVelo);
playerAnimator.SetTrigger("DefTurn");
}
private float calculateGravity()
{
return Physics2D.gravity.y / -2;
}
}