- Home /
 
Problems with scripts
I'm having some troubles with the following script:
 #pragma strict
 
 var distHeroi : Vector3; 
 var velocidadeTranlate: float;
 var distanciaMinima: float;
 var normX: float;
 var normZ: float;
 var velocidadeRotacao : float;
 var tempo : float;
 var cor : float;
  
 
 function Start () {
 distanciaMinima = 5;
 tempo =0;
 }
 
 function Update () {
 
 velocidadeTranlate = 1 * Time.deltaTime;
 
 distHeroi = dude.distHeroi - transform.position;
 normX = distHeroi.x;
 normZ = distHeroi.z;
 
 
 
 if(normX<0) normX = -normX; 
 if(normZ<0) normZ = -normZ;
 
 
 
 if((normX<distanciaMinima) && (normZ<distanciaMinima) )
  {
   //atacar
   transform.rotation.y=0;
   if (distHeroi.x > 0) transform.Translate(velocidadeTranlate,0,0);
   else transform.Translate(-velocidadeTranlate,0,0); 
 
   if (distHeroi.z > 0) transform.Translate(0,0,velocidadeTranlate);
   else transform.Translate(0,0,-velocidadeTranlate); 
   
  }
 else
  {
    //movimento aleatório
    transform.Translate(0,0,velocidadeTranlate);
    tempo = tempo + 1*Time.deltaTime;
    print(tempo);
    if (tempo >3)
    {
      velocidadeRotacao = 360*Random.value; 
      transform.Rotate(0,velocidadeRotacao,0);
      tempo = 0;
    }
  }
 }
 function OnCollisionEnter(collision: Collision)
 {
  if (collision.gameObject.tag == "Ammo")
  {
    spawn.contMaxInim = spawn.contMaxInim -1;
    Destroy(gameObject);
    }
  if (collision.gameObject.tag == "Player")
    {
    vida.vida = vida.vida -10;
    }
 }
 
               1º : the collision function isn't woring.
2º : in the 33º line, I want to make the object that is loading the script follow the object with the script dude, but that isn't happening.
(the var : distHeroi is static)
what is wrong with the script?
To begin with, it will be easier to read like this :p (reworked indentation but I don't know why there are no colors :( )
 #pragma strict
  
 var distHeroi : Vector3; 
 var velocidadeTranlate: float;
 var distancia$$anonymous$$inima: float;
 var normX: float;
 var normZ: float;
 var velocidadeRotacao : float;
 var tempo : float;
 var cor : float;
  
  
 function Start () {
 
     distancia$$anonymous$$inima = 5;
     tempo =0;
 }
  
 function Update () {
  
     velocidadeTranlate = 1 * Time.deltaTime;
  
     distHeroi = dude.distHeroi - transform.position;
     normX = distHeroi.x;
     normZ = distHeroi.z;
  
  
  
     if(normX<0) normX = -normX; 
     if(normZ<0) normZ = -normZ;
  
  
  
     if((normX<distancia$$anonymous$$inima) && (normZ<distancia$$anonymous$$inima) )
      {
           //atacar
           transform.rotation.y=0;
           if (distHeroi.x > 0) transform.Translate(velocidadeTranlate,0,0);
           else transform.Translate(-velocidadeTranlate,0,0); 
          
           if (distHeroi.z > 0) transform.Translate(0,0,velocidadeTranlate);
           else transform.Translate(0,0,-velocidadeTranlate); 
  
      }
      else
      {
            //movimento aleatório
            transform.Translate(0,0,velocidadeTranlate);
            tempo = tempo + 1*Time.deltaTime;
            print(tempo);
            if (tempo >3)
            {
                 velocidadeRotacao = 360*Random.value; 
                 transform.Rotate(0,velocidadeRotacao,0);
                 tempo = 0;
            }
      }
 }
 
 function OnCollisionEnter(collision: Collision)
 {
      if (collision.gameObject.tag == "Ammo")
      {
            spawn.cont$$anonymous$$axInim = spawn.cont$$anonymous$$axInim -1;
            Destroy(gameObject);
      }
      if (collision.gameObject.tag == "Player")
      {
           vida.vida = vida.vida -10;
      }
 }
 
                  You say collision is not working? you mean it is not called at all? or not doing what you want? I see you want to destroy an object, which one is it : the one with this script or the one that collided?
regards
Also it would be nice to know on what this script is attached :p
Your answer
 
             Follow this Question
Related Questions
Learn Scripting Easy? 5 Answers
Objectives based on object appear. 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
How to toggle a key for a car to go forward or backward? 1 Answer