using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CannonManager : MonoBehaviour
{
public GameObject cannonBallPrefab;
public Transform firePoint;
private Camera _cam;
private bool _pressingMouse = false;
private Vector3 _initialVelocity;
void Start()
{
_cam = Camera.main;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
_pressingMouse = true;
if (Input.GetMouseButtonUp(0))
{
_pressingMouse = false;
_Fire();
}
if (_pressingMouse)
{
// coordinate transform screen > world
Vector3 mousePos = _cam.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0;
// look at
transform.LookAt(mousePos);
}
}
private void _Fire()
{
// instantiate a cannon ball
GameObject cannonBall = Instantiate(cannonBallPrefab, firePoint.position, Quaternion.identity);
// apply some force
Rigidbody rb = cannonBall.GetComponent<Rigidbody>();
rb.AddForce(_initialVelocity, ForceMode.Impulse);
}
}