Created
April 21, 2020 13:53
-
-
Save openroomxyz/7809190fc89db536e6af72b770a1e903 to your computer and use it in GitHub Desktop.
Unity : Can i see Raycast example?
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 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