Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by InvalidNickname · Jun 24, 2014 at 11:35 AM · javascript

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nerevar · Jun 24, 2014 at 03:02 PM 0
Share

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

avatar image Nerevar · Jun 24, 2014 at 03:15 PM 0
Share

Also it would be nice to know on what this script is attached :p

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges