- Home /
Turret Shooting
I'm making a turret in my game and I want it to rotate 180 degres while shooting at me I have a machine gun script:
var fireRate : float = 0.1;
var Prefab: Transform;
private var nextFire = 0.0;
var speed : float = 50;
function Update () {
if(Input.GetKey("mouse 0")&&Time.time > nextFire){
nextFire = Time.time + fireRate;
var copy = Instantiate(Prefab,GameObject.Find("spawnPoint").transform.position,transform.rotation);
copy.rigidbody.velocity = transform.TransformDirection(Vector3.forward * speed);
}
}
a regular gun script:
var fireRate : float = 0.1;
var Prefab: Transform;
private var nextFire = 0.0;
function Update () {
if(Input.GetKeyDown("mouse 0")&&Time.time > nextFire){
nextFire = Time.time + fireRate;
var copy = Instantiate(Prefab,GameObject.Find("Spawn Point").transform.position,transform.rotation);
copy.rigidbody.velocity = transform.TransformDirection(Vector3.forward * 100);
}
}
I tried altering both but I cant figure out how to make a turret script. Thanks in advance!
Answer by Loius · Jun 21, 2012 at 03:21 PM
transform.LookAt(player.position);
if (Time.time > nextFire) Fire();
Not sure what you mean by 180*, but if you mean to limit the object's rotation you'll need to do logic after the LookAt to constrain the rotation's euler angles.
That's... that's a beautiful response.
Something like this, which is untested, just an example of what logic i mean:
x = transform.eulerAngles.x;
if ( x > 180 ) x = 180;
if ( x < 0 ) x = 0;
transform.eulerAngles.x = x;
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Turret Script Problem 2 Answers
Turret Range Script 2 Answers
AddComponent adds too many (C#) 3 Answers
turret AI question 3 Answers