• 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
Question by b3agz · Sep 18, 2013 at 03:46 PM · transform.positionanimator controllerchildnpcontriggerexit

Is Attachment by Transform.Position Good Practice?

Hi. As with most of the askers here, I'm fairly new to all this.

Basically, I wanted to make a simple NPC trigger for initiating dialogue when the player is close enough. I originally made a trigger mesh (cylinder) as a child of the NPC, and put some onTriggerEnter/onTriggerExit code on it.

The code worked fine for entering, but hardly ever for exiting. After an evening of Googling, it seems there is some kind of bug with onTriggerExit (or, at least, a lot of people aren't doing it right, myself included), and I also found that when I disabled the Animator on my NPC, onTriggerExit worked fine.

So, my solution was to unchild the trigger cylinder and stick—

 transform.position = NPC.transform.position;

—in the trigger's update function, which fixed the problem; it now works perfectly.

So, my question is this: is this method likely to cause any problems I might be missing due to my noobiness? Is more resource intensive, for example, or is there potential for the trigger cylinder to lag behind a moving NPC?

Thank you for any help.

Comment

People who like this

0 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 perchik · Sep 18, 2013 at 04:00 PM 0
Share

Perhaps you should elaborate more. Setting something's position isn't a bad thing to do, but I'm confused as to what you want to do OnTriggerExit and what you are doing now.

avatar image b3agz · Sep 18, 2013 at 05:45 PM 0
Share

It was just a matter of onTriggerEnter—do some dialogue stuff, onTriggerExit—deactivate dialogue stuff. The code in the onTrigger functions is exactly the same, I've just unparented the collider from the object it represents (the NPC), as, for some reason that is beyond me, having an active Animator stopped onTriggerExit working.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by dannyskim · Sep 18, 2013 at 04:18 PM

Having an update like this is not terribly expensive. In this case, I think you're worrying about optimization a little too much.

Just make sure to cache both of the transforms and used the cached references instead of a direct lookup and it should be perfectly fine to do that.

Comment

People who like this

0 Show 3 · 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 b3agz · Sep 18, 2013 at 05:50 PM 0
Share

Thanks. I just wanted to be sure. Just for clarification, when you say cache instead of direct lookup, do you mean putting the object into the script, ie:

 public transform NPC;

as opposed to something like;

 NPC = GameObject.Find("NPC");

or did you mean something else?

avatar image dannyskim · Sep 18, 2013 at 09:57 PM 0
Share

Something along those lines. NPC specifically would probably already be a cached object, what I was more pointing too was accessing transform every frame.

Instead, you should op to cache the transform if you're going to be doing a lot of lookups on it:

 private Transform _transform;
 
 void Start()
 {
     _transform = transform;
 }
 
 void Update()
 {
     _transform.position = NPC.transform.position;
 }

even a transform.position lookup does GetComponent behind the scenes, so caching it will be best for performance reasons.

avatar image b3agz · Sep 18, 2013 at 09:59 PM 0
Share

Ahh, thanks. I didn't know that.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Make animation go to child.,Make animation go to child 1 Answer

object's children not moving with it when using a Lerp-ing coroutine 0 Answers

Set Position and Rotation with another GameObject as Pivot (Script) 0 Answers

Animator parameters 1 Answer

How to handle multiple animation variants 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