Skip to content

Instantly share code, notes, and snippets.

@openroomxyz
Created April 21, 2020 13:53
Show Gist options
  • Save openroomxyz/7809190fc89db536e6af72b770a1e903 to your computer and use it in GitHub Desktop.
Save openroomxyz/7809190fc89db536e6af72b770a1e903 to your computer and use it in GitHub Desktop.
Unity : Can i see Raycast example?
public class Weapon : MonoBehaviour
{
[SerializeField] Camera FPCamera;
[SerializeField] float range = 100f;
[SerializeField] float damage = 30f;
void Update()
{
if(Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
private void Shoot()
{
RaycastHit hit;
if(Physics.Raycast(FPCamera.transform.position, FPCamera.transform.forward, out hit, range))
{
print("I hit this thing " + hit.transform.name);
//TODO : add some hit effect for visual players
EnemyHealth target = hit.transform.GetComponent<EnemyHealth>();
if(target != null)
{
target.TakeDamage(damage);
}
}
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment