- Home /
how to combine my move script and my shoot script into 1 script
HI , Can someone please help , I Have 2 Javascripts, I am trying to combine the 1st Script Makes the Enemy follow and Look at the Player. The 2nd Script have the Enemy shoot at the Player and also follow the player when the player is in range. But the scripts doesn't work well together I want to combine them into one script.
Script # 1 ( The Follow Script) ------------------
var Player : Transform; var MoveSpeed = 4; var MaxDist = 10; var MinDist = 5;
function Start () {
}
function Update () { transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.posi tion) >= MinDist){
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.posi tion) <= MaxDist) { //Here Call any function U want Like Shoot at here or something }
} }
Script #2 The shooting script_
var player : Transform; var safeDist : float = 15; var currentDist : float; var shooting : boolean = false; var bulletFab : Rigidbody; var power : float = 1000; var shotDelay : float = 1;
function Update () {
currentDist = Vector3.Distance(transform.position, player.position);
if(currentDist < safeDist){ transform.LookAt(player); if(!shooting) shootStuff();
}
}
function shootStuff(){
shooting = true;
var fwd = transform.TransformDirection(Vector3.forward); var bulletShot : Rigidbody = Instantiate(bulletFab); bulletShot.AddForce(fwd * power);
yield WaitForSeconds(shotDelay); shooting = false;
}
If you select the code in your question and click the button with 1's and 0's on it, it will indent it, so I can read it. Do this and I will try to help you.
Answer by MWW · Dec 18, 2012 at 11:22 AM
`enter code here`
var player : Transform; var safeDist : float = 15; var currentDist : float; var shooting : boolean = false; var bulletFab : Rigidbody; var power : float = 1000; var shotDelay : float = 1;
function Update () {
currentDist = Vector3.Distance(transform.position, player.position);
if(currentDist < safeDist){ transform.LookAt(player); if(!shooting) shootStuff();
}
}
function shootStuff(){
shooting = true;
var fwd = transform.TransformDirection(Vector3.forward); var bulletShot : Rigidbody = Instantiate(bulletFab); bulletShot.AddForce(fwd * power);
yield WaitForSeconds(shotDelay); shooting = false;
}
Answer by MWW · Dec 18, 2012 at 11:21 AM
var Player : Transform; var MoveSpeed = 4; var MaxDist = 10; var MinDist = 5;
function Start () {
}
function Update () { transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.posi tion) >= MinDist){
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.posi tion) <= MaxDist) { //Here Call any function U want Like Shoot at here or something }
} } __________________________________________________ _____________________________________________
Answer by MWW · Dec 18, 2012 at 11:21 AM
var Player : Transform; var MoveSpeed = 4; var MaxDist = 10; var MinDist = 5;
function Start () {
}
function Update () { transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.posi tion) >= MinDist){
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.posi tion) <= MaxDist) { //Here Call any function U want Like Shoot at here or something }
} } __________________________________________________
var player : Transform; var safeDist : float = 15; var currentDist : float; var shooting : boolean = false; var bulletFab : Rigidbody; var power : float = 1000; var shotDelay : float = 1;
function Update () {
currentDist = Vector3.Distance(transform.position, player.position);
if(currentDist < safeDist){ transform.LookAt(player); if(!shooting) shootStuff();
}
}
function shootStuff(){
shooting = true;
var fwd = transform.TransformDirection(Vector3.forward); var bulletShot : Rigidbody = Instantiate(bulletFab); bulletShot.AddForce(fwd * power);
yield WaitForSeconds(shotDelay); shooting = false;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613085609im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
My asteroid dont move after shoot - why? 1 Answer
Shoot a bomb to mouse position smooth 1 Answer
enemy shoot AI precision 1 Answer
Auto shooting script. 1 Answer
AI problems ( enemy doesn't shoot ) 2 Answers