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 Grug16 · Jun 19, 2012 at 12:59 AM · transformvector3normalize

Inefficient transform code.

I've developed this code to constantly place the object between the guardedObject and player at a specific distance. It's functional, however doing so makes the game very, very slow. Is there a more efficient way to implement this?

 void Update () {
         
         Vector3 NewPosition = -(guardedObject.transform.position - player.transform.position);
         transform.localPosition = NewPosition.normalized * guardDistance;
 
         
     }

EDIT: To clarify, guardDistance is a pre-determined float, currently set at 4. I made it a variable so I could adjust the distance if needed. Basically I want this object to always be 4 units away from Guarded Object, directly between it and the Player.

Comment
Add comment · Show 9
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 Wolfram · Jun 19, 2012 at 02:30 AM 0
Share

As @Berenger noted, one or two square roots shouldn't be able to cause your game being "very, very slow". Try the suggestions in both answers and see if that changes anything. Then comment out all three variants to make sure it is truly this code segment that's causing your problems.

avatar image Grug16 · Jun 19, 2012 at 03:13 AM 0
Share

Wolfram's answer below won't work, as lerping will cause the object to move further away from the guardedObject when the player moves away, when I want it to stay the same distance constantly.

I did discover something, though. Berenger's solution worked when sqrt_of_guardDistance was 2. The object would track the player and the game remained performant, but it would stay very close to the guardedObject. Increasing sqrt_of_guardDistance to 6 still had the object stay very close, but now the entire game was much slower.

Perhaps the lag is caused by trying to adjust the object to a very precise location with a high number of decimal places?

avatar image Mizuho · Jun 19, 2012 at 03:16 AM 0
Share

A high number of decimal places shouldn't matter considering float and double values cut off their decimal places past a certain point.

avatar image Eric5h5 · Jun 19, 2012 at 04:00 AM 0
Share

Unless you're running on extremely slow hardware (as in, older hardware than Unity even supports), there's no way this would cause any noticeable lag.

avatar image Berenger · Jun 19, 2012 at 05:34 AM 2
Share

You wouldn't be using print or Debug.Log each frames on that script would you ? That would kick your computer into the precious.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Berenger · Jun 19, 2012 at 01:10 AM

Unless you've got hundreds instances of that script, this won't change much, but you could calculate the sqrt of guardDistance once at start and then use

 transform.localPosition = (NewPosition / NewPosition.sqrMagnitude) * sqrt_of_guardDistance;

That way, you can save a sqrt per frame. You could calculate the direction, the magnitude and the yourself and ignore one component (y), if the game is 2D, so you can save some calculation too. That's hardly gonna change anything but it's the best I can think of. Your problem is probably elsewhere.*

Comment
Add comment · Show 1 · 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 Ipsquiggle · Jun 19, 2012 at 03:59 AM 1
Share
  • for "your problem is probably elsewhere"

avatar image
0

Answer by Wolfram · Jun 19, 2012 at 01:06 AM

.normalized uses a square root, which might be what's slowing your game down. I don't know how you compute guardDistance (probably also using .magnitude or Vector3.Distance(), which both use square roots, too), but you could use this approach instead:

 trnasform.position=Mathf.Lerp(player.transform.position, guardedObject.transform.position, factor);

If factor is 0, object will be placed at player position, if it is 1, it will be placed at guardedObject position, and for values between that it will be...well, between them.

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

8 People are following this question.

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

Related Questions

Guided missile help c# 1 Answer

How to smoothly rotate to certain directions using input axis 1 Answer

How to determine speed of an object when using transform.position 2 Answers

Issue's With Positioning 0 Answers

Move an object forward and not up? 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