- Home /
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.
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);
}
}
Your answer
Follow this Question
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