using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class CannonScript : MonoBehaviour { [SerializeField] private GameObject cannon; [SerializeField] private GameObject projectile; [SerializeField] private float lookDepth; [SerializeField] private float cannonForce; private Vector3 lookDir; // Start is called before the first frame update void Start() { lookDir = Vector3.zero; } // Update is called once per frame void Update() { } private Vector3 GetMouseWorldPosition(){ Vector3 screenPos = Mouse.current.position.ReadValue(); screenPos.z = lookDepth; Vector3 worldPoint = Camera.main.ScreenToWorldPoint(screenPos); return worldPoint; } public void OnLook(InputAction.CallbackContext ctx){ Vector3 bodyLookPos= GetMouseWorldPosition(), cannonLookPos = GetMouseWorldPosition(); bodyLookPos.y = 0; gameObject.transform.LookAt(bodyLookPos); cannon.transform.LookAt(cannonLookPos); lookDir = (cannonLookPos - transform.position).normalized; } public void OnFire(InputAction.CallbackContext ctx){ if(ctx.performed){ GameObject proj = Instantiate(projectile, transform.position, cannon.transform.rotation); proj.GetComponent().AddForce(cannonForce * lookDir, ForceMode.Impulse); } } }