- Home /
Ray Casting Tutorial?
Ive looked everywhere for a simple tutorial on ray casting but all I have found is the script reference page which dosent really help with what I need. But I don't know anything about ray casting. Instead of having a bullet prefab coming out of my gun I have heard a ray cast would be better, can anyone help me with this?
Are you intending to use the raycast for the impact/damage calculation i.e. to tell if it hit an object? Or are you more interested in working out how to represent visually a bullet co$$anonymous$$g out of a gun?
How would you do this if you wanted to visually represent a bullet?
Answer by aldonaletto · Oct 31, 2011 at 11:12 PM
Slow projectiles like rockets or cannon balls may be instantiated. For fast projectiles like bullets, it's better to use a Raycast instead (in the First Person Tutorial, the rockets are instantiated, while the machine gun uses raycast). The script below shows how to use raycast to shoot: when the Shoot function is called, a Raycast is done in the weapon forward direction; if nothing hit, nothing happens; if it's an enemy, you can instantiate blood at the hit point and send a message to make the enemy reduce its health; if another thing is hit, you can emit sparks at the hit point.
var shotSound: AudioClip; // drag a shot sound here, if any var bloodPrefab: GameObject; // drag the blood prefab here, if any var sparksPrefab: GameObject; // drag the sparks prefab here, if any
function Shoot(){ if (shotSound) audio.PlayOneShot(shotSound); // play the shot sound var hit: RaycastHit; if (Physics.Raycast(transform.position, transform.forward, hit)){ // prepare rotation to instantiate blood or sparks var rot = Quaternion.FromToRotation(Vector3.up, hit.normal); if (hit.transform.tag == "Enemy"){ // if enemy hit... if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot); // make it bleed... hit.transform.SendMessage("ApplyDamage", 5, SendMessageOptions.DontRequireReceiver); // and consume its health } else { // otherwise emit sparks at the hit point if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot); } } } This script shoots in the forward direction of the object to which it's attached. If the weapon model has a different orientation, child to it an empty object and align it to the shooting direction, then attach the script to this object.
NOTE: You must add a ApplyDamage function to the enemy, like this:
var health = 100;
function ApplyDamage(damage: int){ health -= damage; if (health
I have done everything you have said but nothing happens; my enemy does not take damage and there are no blood spark effects. I have added the two scripts onto an empty game object and enemy but i cannot get it to work, please can you help?
How are you calling the Shoot function? Usually you should use something like this in the same script where Shoot() is:
function Update(){
if (Input.GetButtonDown("Fire1")){
Shoot();
}
}
The script above should be attached to the weapon, or to an empty object childed to the weapon, and the shooting direction is the "blue arrow" (in the Editor) of this object. The enemy also should be tagged "Enemy".
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
RayCast Loop? 2 Answers
RayCast Delay? 1 Answer
Raycast Help 1 Answer