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 m3chanist · Jun 08, 2021 at 12:19 PM · rotationtransformeulerangles

How do i allow a gameobject to rotate along with another gameobject?

I have two gameobjects. They are seperate and do not have a child/parent relationship. One of the objects rotates on its local y-axis with a slider (at 215 degrees in the screenshot). How do i make the other gameobject rotate the same amount but with the same axis so it turns with the object? In the screenshot it is stationary and hasnt turned 215 degrees with the first object?

My code so far:

 public enum ArmBlock {A,B,C}
 
 public class ArmController : MonoBehaviour
 {
     public ArmBlock Arm;
     public GameObject Predecessor;
     public GameObject SizeInput;
     float Sizeval;
 
     // Update is called once per frame
     void Update()
     {
        
        switch(Arm){
            case ArmBlock.A:
                 
            break;
            case ArmBlock.B:
                 transform.position = new Vector3(Predecessor.transform.position.x-0.5f,Predecessor.transform.position.y+Predecessor.transform.localScale.y/2,Predecessor.transform.position.z);
                 //transform.RotateAround (new Vector3(5,2 ,0), new Vector3(1,0,0),90*Time.deltaTime);
            break;
            case ArmBlock.C:
            break;
        }
     }
 
     public void ModifySize(){
         Sizeval = float.Parse(SizeInput.GetComponent<Text>().text);
         gameObject.transform.localScale = new Vector3(transform.localScale.x,Sizeval,transform.localScale.z);
         transform.position = new Vector3(transform.position.x, Predecessor.transform.position.y + Sizeval/2, transform.position.z);
     }
 
     public void Rotate(float position){
         transform.eulerAngles = new Vector3(transform.eulerAngles.x, position, transform.eulerAngles.z);
         GameObject.Find("A rotation value").GetComponent<Text>().text = position.ToString();
     }
 }


This script is attached to each object. The first object is A, the second B (the one im trying to rotate with A). Predecessor holds the previous object so this script on B it holds A and for A it holds nothing. alt text

capture.jpg (37.7 kB)
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 Nefisto · Jun 08, 2021 at 02:28 PM

@m3chanist
If they don't have any hierarchy relation you'll need some kind of manual control about their rotation behavior, you said that want them to rotate "together" but your code is changing one position based on the predecessor scale so I didn't get the idea, IDK what you are creating in the big picture but at a first glance I think that a good approach is to make events for rotation in each one and make sure that other register in this events, or maybe create one "Rotate method" and make sure to call it in predecessor if you have one.

I'll add code in edit, working on it now, if want to reach me up you can add me on discord Nefisto#3403 and we can talk about it

Edit: tbh the event part will only helps if this scale, otherwise you can only make sure to call public behaviors on predecessor, something like that

 public class Box : MonoBehaviour
 {
     // We'll rotate every time that predecessor rotates
     public Box predecessor;
 
     // register this in slider
     public void Rotate (float yaw)
     {
         Rotate(new Vector3(0f, yaw, 0f));
         
         if (predecessor)
             predecessor.Rotate(yaw);
     }
     
     private void Rotate (Vector3 targetRotation)
     {
         transform.Rotate(targetRotation);
     }
 }


alt text


capture.gif (278.8 kB)
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

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

How to get Angle float of specific axis.(Turret clamping related). 2 Answers

Set a relative (custom) speed to a rotation 1 Answer

Problems caused by non-unique Euler angle solutions 1 Answer

Crashing after trying to rotate a GameObject 1 Answer

Use a single rotation axis of a freely rotating object 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