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 ozgunn · Oct 25, 2020 at 10:35 AM · parent-childattach3dobject

Attaching 2 object

alt textI need help about attaching a cylinder and a sphere. I'm trying to swing that cylinder by rotating sphere but when I rotate sphere cylinder doesn't get affected.

 public GameObject cylinder;
     public Transform sphere;
     public void yelpaze(Transform newParent)
     {
         cylinder.transform.SetParent(newParent);
         cylinder.transform.SetParent(newParent, false);
         cylinder.transform.SetParent(null);
     }
 

I tried this after an advice but still doesn't work.

ekran-goruntusu-2020-10-25-134043.jpg (21.9 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 Hellium · Oct 25, 2020 at 11:08 AM 1
Share

cylinder.transform.SetParent(newParent); should be enough. cylinder.transform.SetParent(null); detaches the cylinder from its parent.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Duckocide · Oct 25, 2020 at 11:45 AM

Hi @ozgunn

I'm not sure what your method is trying to achieve as your assigning then un-assigning the parent transform for the cylinder. Any hoo, I'm guessing you're trying to get the cylinder to swing back and forth under the sphere.

You can of course make the cylinder a child of the sphere in the editor. Here goes.

alt text

  1. Add the sphere to your scene

  2. Add the cylinder to your scene and make it a child of the sphere (drag the cylinder over the sphere) - This is the same as the SetParent code you have written.

  3. Adjust the cylinder position to where you want it.

  4. Add the script below to the sphere

  5. Run

public class SwingCylinder : MonoBehaviour {

     private Vector3 _originalLocalEulerAngles;
 
     // Start is called before the first frame update
     void Start()
     {
         _originalLocalEulerAngles = transform.eulerAngles;
     }
 
     // Update is called once per frame
     void Update()
     {
         // In start() we grabbed the current angles for this gameObject. So, we can now add a sinusoidal (pendulum) swing to them
         // on the z-axis (Vector3.forward) ... I.e. Axis simplistically through the screen (in world space)
         // The formula basically uses the prevailing time in seconds as a radian value in the Sin (Sine function). 
         // We multiply this by 60 (degrees) so the swing oscillates between -60 and 60
         transform.eulerAngles = _originalLocalEulerAngles + (Vector3.forward * Mathf.Sin(Time.time) * 60f);
 
     }
 }
 ![alt text][2]


[2]: /storage/temp/169836-editor3.jpg


editor1.jpg (8.9 kB)
editor3.jpg (130.5 kB)
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 ozgunn · Oct 25, 2020 at 12:43 PM 0
Share

thx very much @Duckocide. Even I don't know why I have written that code. But now I get it how to swing my object thanks to you.

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

138 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 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

Adding A Script To A Scene 2 Answers

Attach a GUI Skin Dynamicaly 2 Answers

How to 'gravitize' an object/camera to another sphere... 1 Answer

How do i create an object with a GUI Button 1 Answer

Attaching an object to a camera movement weird, Help! 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