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]
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]
[1]: /storage/temp/169834-editor1.jpg
[2]: /storage/temp/169836-editor3.jpg
↧