- Home /
having problems with my raycast shooting script
well i have been working on this raycast shooting script and it is giving me some errors. i am a beginner at scripting, so i am having trouble figuring out what is wrong, here is the script
var bulletTex : GameObject[];
function Update () {
var RayHit : boolean;
var fwd = transform.TransformDirection(Vector3.forward);
var hit : RayHit;
Debug.DrawRay(transform.position, fwd * 10, Color.red);
if (Input.GetButtonDown("fire1")) {
Shoot();
}
function Shoot() {
if (Physics.Raycast (transform.position, fwd, Reach)&&hit.transform.gameObject.tag == "Enemy") {
RayHit = true;
}
else {
RayHit = false;
}
if (RayHit==true) {
Damage();
}
if (RayHit==false) {
Instantiate(bulletTex[Random.Range(0,3)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
}
}
function Damage() {
hit.transform.SendMessage("DecreaseHealth");
}
}
What errors do you get?
The following is wrong:
if (Physics.Raycast (transform.position, fwd, Reach)&&hit.transform.gameObject.tag == "Enemy")
I think you want something like this:
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit) == true)
{
if(hit.transform.gameObject.tag == "Enemy")
{
// Enemy is hit.
}
}
BCE0044: expecting (, found 'Shoot'. this error is on line 17
UCE0001: ';' expected. Insert a semicolon at the end. this error is on line 17
BCE0044: expecting (, found 'Damage'. this error is on line 36
UCE0001: ';' expected. Insert a semicolon at the end. this error is on line 36
You've declared 3 variables in Update() so only this method can access those and look at the names/types you gave them:
var RayHit : boolean; var hit : RayHit; // ERROR
It's RaycastHit not RayHit so you should change that.
I dont know how it's in JS[US] but in C# you have to point where hit data will be "dumped" and you haven't specified it:
var ray : Ray = new Ray(position, direction); var hit : RaycastHit; if(Physics.Raycast(ray, out hit, distance)) { DoSomeLogcHere(); }
i sorted it out watched a couple of tutorials to better explain raycast shooting thanks alot for helping me out.
Your answer
Follow this Question
Related Questions
need help converting gun script from c# to javascript 1 Answer
Raycast shooting 1 Answer
im having trouble with raycast shooting script 0 Answers
Fall Damage Script Problem? 1 Answer
musket gun script is not working 3 Answers