Skip to content

Instantly share code, notes, and snippets.

@Matthew-J-Spencer
Created March 10, 2021 11:55
Show Gist options
  • Save Matthew-J-Spencer/81ac0dfaf3cfc698b69a5120e1c6df4b to your computer and use it in GitHub Desktop.
Save Matthew-J-Spencer/81ac0dfaf3cfc698b69a5120e1c6df4b to your computer and use it in GitHub Desktop.
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