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 BilboStabbins · Jan 06, 2013 at 08:19 PM · lerpchildmovedeltatimetranslation

Translating child to the right whilst maintaining parent's forward movement

Hi,

I have a child object that I would like to translate to the right by a set distance when it enters a trigger. The child is directly in front of its parent which is translating forwards. I would like to be able to move the child to the right by a set distance without affecting the distance between the parent and child (along Z) after the move - that is, I would like the child to continue moving forward with the parent whilst moving to the new position.

What's happening with my code below is that when the child begins moving to the right it falls behind the parent and stays there. The script for the parent's forward translation is on the parent object. The movement is currently executed using a mouse key press to simulate the trigger.

Any guidance would be great. Thanks

 public bool moving = false;

 private Vector3 PointA;

 private Vector3 PointB;

 public float time;

 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         PointA = transform.position;

         StartCoroutine(MoveFromTo(PointA, PointB, time));

         Debug.Log("Mouse button pressed");
     }
 }

 IEnumerator MoveFromTo(Vector3 pointA, Vector3 pointB, float time)
 { 
     if (!moving)
     {
         moving = true;

         // Move 10 units to the right - better to use Vector3.right ? 
         Vector3 RightPos = new Vector3(10f, 3, transform.position.z);
 
         float t = 0f;
 
         while (t < 1f)
         {                 
             // sweeps from 0 to 1 in time seconds
             t += Time.deltaTime / time;
 
             pointB = RightPos;
 
             // set position proportional to t
             transform.position = Vector3.Lerp(pointA, pointB, t);
 
             // leave the routine and return here in the next frame
             yield return 0;
         }

         // finished moving
         moving = false;
     }
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · Jan 06, 2013 at 10:07 PM

If you're moving the child relative to the parent, you should use transform.localPosition instead of transform.position. localPosition is the gameobject's position relative to its immediate parent, while position is in global space. You also should only modify on the axes that you absolutely need to, and that you can't inherit from the parent, I think you'll have no problems then.

EDIT: After a good night's sleep, I looked at this again and this is what you can do:

 void LateUpdate()
 {
     if (Input.GetMouseButtonDown(0))
     {
         PointA = transform.localPosition;
         StartCoroutine(MoveFromTo(PointA, PointB, time));
         Debug.Log("Mouse button pressed");
     }
 }
 
 IEnumerator MoveFromTo(Vector3 pointA, Vector3 pointB, float time)
 { 
     if (!moving)
     {
         moving = true;
 
         // Move 10 units to the right - the addition will be local, so this is fine
         Vector3 RightPos = new Vector3(10f, 3, 0);
 
         float t = 0f;
 
         while (t < 1f)
         {                 
             t += Time.deltaTime / time;
 
             pointB = pointA + RightPos;
 
             transform.localPosition = Vector3.Lerp(pointA, pointB, t);
 
             yield return 0;
         }
 
         moving = false;
     }
 }

Note that I put this in LateUpdate() to make sure it runs after the parent's Update() is done in each frame.

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

First person controller not moving properly after rotation 1 Answer

Stop a Lerp from looping 3 Answers

Lerping the GUI, problem with doing it again 1 Answer

Inversing fixed delta time 1 Answer

Move an object through a set of positions 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