x


Turret that shots a raycast to detect my character then shots at him.

Hello everyone im New to UnityAnswers and im glad i found this place because i need some help with a few Javascripts. I'm pretty new to Scripting so i will explain the best i can.

WHAT I WANT TO HAPPEN: I created a Turret that i want to use a raycast to look for my Character. When it spots my Character i want the Turret to shot me EVEN IF i walk around it. I also want the Turret to stop shooting at me when i leave its raycast or walk behind a wall.

WHAT IS HAPPENING: When i enter the Turrets raycast it does see me, and it looks at me. But it doesnt shot at me and it still detects me through walls.

Heres my script im trying to use PLEASE HELP

var area:int =100; var target: Transform; var damp = 6.0; var bullitPrefab : Transform; var savedTime=0; var AttackSpeed=1000; function Update (){ var layermask= 1 << 8;

 var left = (transform.TransformDirection (-Vector3.right));
 var right = (transform.TransformDirection (Vector3.right));
 var up = (transform.TransformDirection (Vector3.forward));
 var down = (transform.TransformDirection (-Vector3.forward));

 if (Physics.Raycast(transform.position,left,area,layermask)){
 transform.LookAt( target );
 Debug.DrawLine(transform.position,target.position, Color.red);
 return;
 }


 if (Physics.Raycast(transform.position,right,area,layermask)){
 transform.LookAt( target );
 Debug.DrawLine(transform.position,target.position, Color.green);
 return;
 }

 if (Physics.Raycast(transform.position,up,area,layermask)){
 transform.LookAt( target );
 Debug.DrawLine(transform.position,target.position, Color.blue);
 return;
 }


  if (Physics.Raycast(transform.position,down,area,layermask)){
 transform.LookAt( target );
 Debug.DrawLine(transform.position,target.position, Color.yellow);
 return;
 }
 }


 if(target)
 {

 var seconds : int = Time.time;
 var oddeven = (seconds % 2);

 if(oddeven)
 {
 shoot(seconds);
 }

}

function shoot(seconds) { if(seconds!=savedTime) {

var bullit = Instantiate(bullitPrefab ,transform.Find("spawnPointA").transform.position , Quaternion.identity);


 bullit.gameObject.tag = "enemyProjectile";
bullit.rigidbody.AddForce(transform.forward * AttackSpeed);

savedTime=seconds;

}

}

more ▼

asked Mar 09 '11 at 03:07 AM

chrisPOD gravatar image

chrisPOD
26 5 5 8

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

You don't have a function, to start with... And, just a personal thing, its a little unorganized... But thats not important. If I were you I would add "function Update () {" to the top, and see what it does.


-Hope I helped

-Gibson

more ▼

answered Mar 09 '11 at 04:50 AM

AVividLight gravatar image

AVividLight
1.9k 68 100 128

I do have a Function Update look at the top. it says var area:int =100; var target: Transform; var damp = 6.0; var bullitPrefab : Transform; var savedTime=0; var AttackSpeed=1000; function Update (){ var layermask= 1 << 8;

Its just that the script copied over really weird and some the script is gray and not in the little script box thing.

Mar 09 '11 at 12:23 PM chrisPOD

Oh, okay... Sorry... I'll see what else I can do.

Mar 09 '11 at 04:34 PM AVividLight

You can highlight your code and then click the code button (again) and it will put it all in the correct places

Mar 09 '11 at 04:35 PM AVividLight

Oh okay thank you

Mar 09 '11 at 08:17 PM chrisPOD
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5098
x3465
x1536
x181
x172

asked: Mar 09 '11 at 03:07 AM

Seen: 919 times

Last Updated: Mar 09 '11 at 03:07 AM