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 ticTek · Sep 07, 2013 at 07:58 PM · positiondestroyfunction update

gameObject not destroyed after reaching desired coordinates.

pragma strict

 var speed : int = 10;             //velocity of bullet
 var upLimit : float = 5.0;
 
 
 function Start () {
 
 }
 
 function Update () {
     
     transform.Translate(Vector3(0,0,speed*Time.deltaTime));
     Debug.Log(transform.position.z);
     if (transform.position.z >= upLimit){
         Destroy(gameObject);
         
     }
 }


Hello guys. WhatI am trying to do is ro destroy my bullet after it reaches 5.0 or any desired coordinates in the grid. in the z axis. the code doesn't seem to work. Is it a semantics error? Im a beginner coder obviously. some help is greatly appreaciated.

thanks :)

Comment
Add comment · Show 3
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 meat5000 ♦ · Sep 07, 2013 at 08:21 PM 0
Share

Tried

 transform.Translate(Vector3.forward * speed * Time.deltaTime);

?

Shouldnt really make a difference, just worth a go.

avatar image tanoshimi · Sep 07, 2013 at 08:47 PM 0
Share

That code works for me when the script is attached to a simple cube in a brand new scene - I get a debug log of increasing positions, and then the object is destroyed. One thing I noticed - the "z" axis points forward in Unity, but the variable limit against which you're testing is called "UpLimit" - you do know that this moves the object forward, not up?

avatar image ticTek · Sep 07, 2013 at 08:52 PM 0
Share

hi thanks for the tip on simplifying the code.$$anonymous$$aybe i wasn't clear with my question.

when transform.position.z => 5.0 lets say in the z axis. the gameObject is not destroyed?

when i debug the transform.Translate, it clearly traveled from 0 to infinity... technically it the object should be destroyed once it pass the 5.0 mark on the z axis right?

I could add a bumper and make it destroy on collision.

im just curious if i can do it the way i setup the code. or is there another better way to it?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by EHogger · Sep 07, 2013 at 08:58 PM

The problem is that transform.translate moves the object on it's own local axis, but transform.position is in world space.

You need to add Space.World to the translate line. See here:

http://docs.unity3d.com/Documentation/ScriptReference/Transform.Translate.html

However, this is only going to move your bullet in one direction. If you wanted to move the bullet in it's own axis and limit the range, you'd need to store the starting position and measure the distance travelled instead.

Comment
Add comment · Show 2 · 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
avatar image ticTek · Sep 07, 2013 at 09:13 PM 0
Share

exactly what i was looking for. how could i have missed that in the Ref. maybe i skimmed through it, will need to read carefully next time round!

thanks a lot! :)

avatar image EHogger · Sep 07, 2013 at 09:47 PM 0
Share

No problem. Remember to accept ;)

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

18 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

problem whit Instantiate Prefabs position. 0 Answers

Can I utilize LoadLevelAdditive for immersive game? 2 Answers

Camera rotation around player while following. 6 Answers

C#: Changing current position of an object in y. 1 Answer

Remove object based on its position alone 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