- Home /
What's wrong with this (Java)Script? Some parts don't work
Hi
I was programmin a simple script, when I test it I note that some parts din't work. Here's the Script:
var Velocidad = 0.2;
var Rango = 30;
var RangoMeta = 5;
var RangoAmigo = 10;
var Meta = true;
var Disparar = false;
var Estado = 0;
//--------------------------------------------------------------------------------------------------
//These variables are for the machine gun, this part of the script is no mine
public var PuntoDisparo : Transform; public var Arma : GameObject; public var Aceleracion : float = 1; public var Disparo : GameObject; var FireRate = 0.5; var ClipSize = 30; var ReloadTime = 0.5; private var Reload = false;
function Start(){ var ProrArmas : AutoDestruir = Arma.GetComponent(AutoDestruir); Aceleracion = ProrArmas.Aceleracion; }
//-------------------------------------------------------------------------------------------------
function Update () {
//I look for the transform of a game object tagged "Finish2"
var Objetivo = GameObject.FindWithTag("Finish2");
var PosicionFinish = Objetivo.transform;
//Here I look for the transform of a game object tagged "Player"
var Amigo = GameObject.FindWithTag("Player");
var PosicionAmigo = Amigo.transform;
//And here I look for the transfor of a game object tagged "Enemy
var Enemigo = GameObject.FindWithTag("Enemy");
var PosicionEnemigo = Enemigo.transform;
//--------------------------------
if(Meta == true){ transform.LookAt(PosicionFinish); }
if(Vector3.Distance(PosicionFinish.position, transform.position)
Estado = 1;
}
else{
Estado = 0;
}
if(Vector3.Distance(PosicionEnemigo.position, transform.position)
}
else{
Estado = 0;
}
if(Estado == 0){
Meta = true;
transform.Translate(0, 0, Velocidad);
Disparar = false;
} if(Estado == 1){
transform.Translate(0, 0, 0);
} if(Estado == 2){
transform.Translate(0, 0, 0);
transform.LookAt(PosicionEnemigo);
Meta = false;
Disparar = true;
}
if(Disparar == true)
{
FireRate+=0.5;
}
if(FireRate == 2) { FireRate-= 2; }
if(Disparar == true && FireRate == 1){
//This is for instantiate the game object that is the bullet
var CloneFire : GameObject = Instantiate (Arma, PuntoDisparo.position, gameObject.transform.rotation);
CloneFire.rigidbody.AddRelativeForce(new Vector3(0, 0, Aceleracion), ForceMode.VelocityChange);
//And this is for instantiate a particle system that is the muzzle flash
var CloneDisparo : GameObject = Instantiate (Disparo, PuntoDisparo.position, gameObject.transform.rotation);
}
}
I have three objects in the game: The first one is the enemy, tagged Enemy, and has no scripts. The second one is the "player", tagged Player and has the previous script y finally there is the third one, tagged Finish2.
What I want is that my player moves continuously to Finish2, but, when it find an enemy close it stops, stop looking at Finish2, look at Enemy and shoot to it, it works; what doesn't work is the part that says that my object (Player) must stop when be near the Finish or another game object tagged Player.
There is no errors detected by Unity, and I tried a lot of posibilities to resolve this problem but I could not.
Finally, sorry for my spelling, I'm learning English yet.
Your answer
![](https://koobas.hobune.stream/wayback/20220612220233im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Exception error 1 Answer
Is not a Member of Collisions 2 Answers
Trouble converting player controller from JS to C# 1 Answer
Change gameobject sprite with button 1 Answer
Error Message Coordinates 1 Answer