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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Chaos-Fusion · Apr 18, 2013 at 02:16 AM · destroyifignorestatement

Destorying despite IF Statement

I'm new to Unity so this may sound quite trivial to others. Basically, I'm attempting to create a small mine object that moves towards the player when he is within range and then explodes. Through some research and modifying, I found some coding that allowed me to make the mine wait until the player is within range and then charge towards him. When the Mine detects the player, the mine has a 5 second timer before it explodes/destroyed. My main issue is that the mine will destroy regardless of the distance between the Player and the Mine. Below is coding I'm using.

     var start: Transform;
     var end: Transform;
     var target : Transform; //the enemy's target
     var moveSpeed = 3; //move speed
     var rotationSpeed = 3; //speed of turning
     var enemydistance =3;
      
     var enemy : Transform; //current transform data of this enemy
     function Awake()
     {
     enemy = transform; //cache transform data for easy access/preformance
     }  
     function Start()
     {
     target = GameObject.FindWithTag("Player").transform; //target the player
     }
     function Update ()     
           {
            //Detect Distance between Player and Mine
             if (  Vector3.Distance( enemy.position, (Vector3.Lerp(start.position, end.position,Time.time) )) < enemydistance  )  {
             
             //rotate to look at the player
             enemy.rotation = Quaternion.Slerp(enemy.rotation,
             Quaternion.LookRotation(target.position - enemy.position), rotationSpeed*Time.deltaTime);
              
             //move towards the player
             enemy.position += enemy.forward * moveSpeed * Time.deltaTime;    
             //Destroy Mine after 5 seconds
             Destroy(gameObject,5) ;
              }
         
             }

Basically, its ignoring the If statement regarding the distance and just destroys it after 5 seconds. Where abouts did I stuff it up and what can I do to fix it?

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 AlucardJay · Apr 18, 2013 at 02:20 AM 1
Share

What is that Lerp doing at line 20 ?!

 if (  Vector3.Distance( enemy.position, target.position ) < enemydistance  )
avatar image Chaos-Fusion · Apr 18, 2013 at 02:36 AM 0
Share

It was part of a script that I found. Originally, when I was creating this script, the $$anonymous$$e would move regardless of distance. As I was looking for ways to prevent that, I came across the line you pointed out to be wrong. Still, I ended up adding the Lerp coding and the $$anonymous$$e wouldn't move until I was within range started working so I left it as that. Anyway, you're line of code fixed this issue so thanks for the help.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sparkzbarca · Apr 18, 2013 at 07:02 AM

what your going to want to do is create the mine object and add a sphere collider to it.

in the editor you will see an option called is Trigger with a checkbox that will make the collider attached to it a trigger instead. Now you can use the functions

 OnTriggerEnter(collider object){
 }
 
 and
 
 OnTriggerExit(collider object)(
 }
 
 to write code for when an object enters the trigger sphere and when it leaves
 
 basically whenever an object enters the sphere around the mine that is the collider you added marked as a trigger unity will automatically Call
 
 OnTriggerEnter(collider objectThatEnteredCollider);
 
 So your mine will get told an object entered and be given the collider of that object so it can do stuff with that object (access its transform, compare and find out what type of object it is etc)
 
 
 so for example you can do a
 
 OnTriggerEnter(collider EnemyCollider){
 
 //check to make sure we found the player
 if(EnemyCollider.gameobject.tag == "player"){
 
 //if it is lets begin moving toward it
 //trackplayer is just a bool we will use in update, if its true we will move the mine //towards the player, if it's false we wont, this is because ontriggerenter is only called //once, the moment the player enters, it isnt called over and over while he stays there. 
 //There is a function called OnTriggerStay but using update is just as effective.
 
 TrackPlayer = true;
 
 //later you can write the OnTriggerExit function, basically it will just be the line
 //TrackPlayer = false, so when they get far enough away you stop tracking towards them
 //TrackPlayer should start out false of course.
 
 //now lets check the distance and see if were close enough to explode using the built in 
 //distance function unity supplies
 
 if(vector3.Distance(mine.transform.position,EnemyCollider.gameobject.transform.position) < ExplosionDistance){
 Destroy(Mine,5);
 //damage player
 }
 
 //close the original if statement
 }
 //close the ontriggerenter statement
 }
Comment
Add comment · Share
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

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

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

|| inside if statement not working. (noob) 3 Answers

if moving statement 1 Answer

How to make several conditions for an if statement? 5 Answers

using rotation in an if statement 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers


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