Hi,
I’m very new to this so please excuse me. I am trying to put together a 2D Platformer and I have set it so that the player will aim towards the mouse, and rotate the arm sprite so that it looks like he is aiming. But I want to limit the angle of the y axis that they can shoot to + or - 45 degrees. I have managed to limit the rotation of the sprite fine, but the bullet can still fire at any angle and I can’t figure out how to clip it.
Any ideas please?
Many thanks
void FireBullet() {
// Clone bullet
GameObject bulletClone;
bulletClone = Instantiate(bulletPrefab, FiringPoint.position, Quaternion.identity) as GameObject;
// Trajectory Tracking
Vector3 target = Camera.main.WorldToScreenPoint(transform.position);
Vector3 vectorToTarget = (Input.mousePosition - target).normalized;
float fireDirection = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
fireDirection = Mathf.Clamp (fireDirection, -45, 45);
// Fire Bullet
bulletClone.transform.rotation = Quaternion.AngleAxis (fireDirection, Vector3.forward);
bulletClone.rigidbody2D.AddForce (vectorToTarget * bulletSpeed);
print (vectorToTarget);
}