Created
March 29, 2011 23:53
-
-
Save tracend/893597 to your computer and use it in GitHub Desktop.
Unity3D: Use raycasts to create projectiles - Source: http://answers.unity3d.com/questions/4867/using-raycast-to-shoot
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
var par : ParticleEmitter; | |
var damage = 2; | |
function Update () { | |
var hit : RaycastHit; | |
// Use Screen.height because many functions (like this one) start in the bottom left of the screen, while MousePosition starts in the top left | |
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Input.MousePosition.x, Screen.height - Input.MousePosition.y,0)); | |
if (Input.GetMouseButtonDown(0)) { | |
if (Physics.Raycast (ray, hit, 100, kDefaultRaycastLayers)) { | |
Instantiate(par, hit.point, Quaternion.LookRotation(hit.normal)); | |
var otherObj : GameObject = hit.collider.gameObject; | |
if (otherObj.tag == "Enemy") { | |
otherObj.GetComponent(typeof(HealthAI)).CanDie(damage); | |
} | |
} | |
} | |
} | |
var aihitPoints : float = 10; | |
function CanDie ( damage : float) { | |
aihitPoints -= damage; | |
if(aihitPoints <= 0 ) { | |
Destroy(gameObject); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment