Created
March 10, 2021 11:55
-
-
Save Matthew-J-Spencer/81ac0dfaf3cfc698b69a5120e1c6df4b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Turret : MonoBehaviour { | |
private Camera _cam; | |
[SerializeField,Range(1,100)] private float _rotationSpeed = 1; | |
[SerializeField] private Projectile _projectilePrefab; | |
[SerializeField] private Transform _spawnPoint; | |
void Awake() | |
{ | |
_cam = Camera.main; | |
} | |
// Update is called once per frame | |
void Update() { | |
var mousePosition = _cam.ScreenToWorldPoint(Input.mousePosition); | |
mousePosition.z = 0; | |
transform.up = Vector3.MoveTowards(transform.up, mousePosition, _rotationSpeed * Time.deltaTime); | |
if (Input.GetMouseButtonDown(0)) { | |
Instantiate(_projectilePrefab, _spawnPoint.position, Quaternion.identity).Init(transform.up); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment