Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 khaledr · Feb 06, 2018 at 01:21 PM · unity 2dchildlocalpositionnested

how to move child object inside a moving parent in 2d using Vector2.MoveTowards()?

The Parent object is a moving rectangle and inside the rectangle there is another rectangle. the parent moving to right continuously and the child rectangle moving up and down.

alt text (I Nested The Child into The Parent)

i need the child move just inside the parent and don't cross the borders of its parent by using Vector2.MoveTowards()?

2.png (8.2 kB)
Comment
Add comment · Show 1
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 khaledr · Feb 06, 2018 at 04:56 PM 0
Share

@crazy$$anonymous$$night @Bunny83 @Eric5h5 @clunk47 @Aurore @fafase @tanoshimi @duck

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Feb 07, 2018 at 12:08 PM

Uhm, just use child.localPosition and use a local position inside the parent space as target point. If you want to prevent the object from leaving or intersecting the border of the parent you just have to choose target points which are inside the parent. You presented this problem in a very generic / abstract way.


Lets assume the pivot of the parent is the center of the parent and the pivot of the child is the center of the child. We also assume that the parent and child objects have a scale of (1,1,1). We further assume that the parent rect has a side length of xS and yS and the child object has a size of xSc and ySc.


That means the local y coordinate inside the parent goes from border to border:`+yS/2` to -yS/2. However since the child has a height of "ySc" we have to subtract half that from the possible target range. So the valid range for the child goes from (yS - ySc)/2 to -(yS - ySc)/2 on the y axis.

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 khaledr · Feb 07, 2018 at 01:17 PM 0
Share

actually my main concern is how to move the child inside the moving parent(regardless of the border crossing). I simply wrote a script that moves the gameobject that attached to specific point :

 public float speed = .5f;
 public Vector2 moveToPoint;
 
 void Update()
 {
     transform.localPosition = Vector2.$$anonymous$$oveTowards(new Vector2(transform.localPosition.x, transform.localPosition.y), 
     new Vector2(transform.parent.transform.position.x + moveToPoint.x, transform.parent.transform.position.y + moveToPoint.y), speed * Time.deltaTime);
 }

assume you attach this script to a non-child object. it simply moves the object from original position to the "$$anonymous$$oveToPoint" point. but if you attach this script to a child object of a moving parent, the "$$anonymous$$oveToPoint" point is far away from the child object. how can i prevent that issue?

avatar image Bunny83 khaledr · Feb 07, 2018 at 04:58 PM 0
Share

That's because you don't use a "local position" inside the parent. You use the world space position of the parent which of course doesn't make any sense. As i said your target point has to be a point relative to the parent space. It looks like you just want:

 transform.localPosition = Vector2.$$anonymous$$oveTowards(transform.localPosition, moveToPoint, speed * Time.deltaTime);

given that movePoint is a relative point inside the parents local space.

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

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

Related Questions

Local position issue 0 Answers

Lerp a Child but 'keep up locally' with Parent 0 Answers

Get Prefab In Nested Prefab 1 Answer

How to get the world position of the edge of an object? 2 Answers

Make a simple tree 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