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
1
Question by TheBangNeedle · Feb 08, 2012 at 02:01 PM · vector3beginnerlerpneweasing

I'm used to Java/Processing, some help with new() please

Hi all, I'm more familiar with Java/Processing, where I do my utmost to avoid creating objects in the main draw loop so the code below feels kinda wrong. Would someone be kind enough to look over my code and tell me if there's a better way of doing what I'm doing below.

I want this object to ease towards the target object on Y but for it to stay fixed at the same x position.

Your feedback would be greatly appreciated, and thanks for your time.

Dave.

 public class follow : MonoBehaviour {
 
 public GameObject target;
 public float easing;
 private Vector3 offset, middle, final;
 
 void Start () 
 {
     offset = target.transform.position - transform.position; 
 }
 
 
 void Update () 
 {
     middle = target.transform.position - offset;
     final = Vector3.Lerp(transform.position, middle, easing);
     transform.position = new Vector3(middle.x, final.y, final.z);
 }

}

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tingham · Feb 08, 2012 at 02:26 PM

Two really obvious options:

  1. Use iTween for your animation and specify a Y parameter only in your MoveTowards

  2. Build your target vector to nullify the x position before you apply easing via Lerp.

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

Answer by CHPedersen · Feb 08, 2012 at 02:39 PM

I agree with your general notion, that one should generally beware of creating new objects in draw loops. But there are a few things you're missing here. :)

Vector3 is not an object. It's a struct. As such, you're creating a new value type each frame, not a reference type. This means creating a new Vector3 behaves the same in terms of memory allocation as writing "classInteger = 5", assuming classInteger is a variable of type integer defined by the class. Surely, no one is going to object to doing that in update.

Note, also, that types declared in the scope of some method (as opposed to the fields of a class) are allocated on the stack, not on the heap. So the memory reserved for such allocation gets popped off the stack again when the method (Update, here) leaves scope, and therefore has no impact on garbage collection.

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

6 People are following this question.

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

Related Questions

Using Lerp in a Constant Moving Object 1 Answer

Not Sure How to Get Around this Problem 1 Answer

Vector3.Lerp making my object invisible. 2 Answers

Object movement with smooth start and end? 2 Answers

Do something when lerp reached target. 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