Find position within rotation of object.

Hi,
This is my first post in unityAnwsers. I am not sure if I framed my question right but what I want is to position an object on a certain point on a rotating object. I do not want the rotating object to have any children and want to make this code based (just for the fun of it… and learning).

Here is a picture explaining what I want :
[33791-rotationposition.png!|33791]

Here is my attempt at it so far. It is not working as intended :

transform.rotation *= Quaternion.Euler (0f,0f, rotationSpeed * Time.deltaTime);

tempObject.transform.position = new Vector3(Mathf.Cos(transform.localRotation.z * Mathf.Deg2Rad) + transform.position.x, Mathf.Sin(transform.localRotation.z * Mathf.Deg2Rad) + transform.position.y, -5f);

The script is attached to the green cube and “tempObject” is a reference to the red sphere.

Any help is greatly appreciated.
Thank You

Maybe you can try the following script. Attach the script to the parent(i.e. the green cube) and set the child(i.e. the red sphere).

using UnityEngine;
using System.Collections;

public class ParentTransform : MonoBehaviour
{
	public Transform child;

	private Transform thisTransform;
	private Vector3 childPositionOffset;
	private Quaternion childRotationOffset;
	private Quaternion originalRotation;
	
	private void Awake()
	{
		thisTransform = this.transform;

		childPositionOffset = child.position - thisTransform.position;
		childRotationOffset = child.rotation * Quaternion.Inverse(thisTransform.rotation);
	
		originalRotation = thisTransform.rotation;
	}
	
	private void Update()
	{
		Quaternion deltaRotation = thisTransform.rotation * Quaternion.Inverse(originalRotation);

		child.position = thisTransform.position + deltaRotation * childPositionOffset;
		child.rotation = thisTransform.rotation * childRotationOffset;
	}
}

This is just a guess as I can’t test it right now but I’ll post it anyways.
If I’m not misunderstanding anything then you just need the position of the cube, the offset of the corner position and the rotation. You should have all of that. Let’s assume your corner is at (1,0,1) from the center of the cube.

Vector3 corner_position_offset = new Vector3(1,0,1);
tempObject.transform.position = transform.position + (transform.rotation * corner_position_offset);

What I’m doing is take the offset rotate it by the rotation of the cube and add the rotated offset to the position of the cube. Should be as simple as that at least in my head :D.

I guess all your answers are correct, but I found the fault in my code. Actually transform.rotation returns a quaternion angle not vector angle, so instead of using ‘transform.rotation.z’ I used ‘transform.rotation.eulerAngles.z’ which gave the rotation in degrees and then converting that to radians gave the correct answer.

Just in case if anybody has the same problem, here is the code :

var zRotation = transform.rotation.eulerAngles.z

//Pythagoras Theorem  
var radius = Mathf.Sqrt(Mathf.Pow(transform.localScale.x/2, 2) + Mathf.Pow(transform.localScale.y/2, 2));

//Subtract 45 degrees to get the corners of the object
tempObject.transform.position = this.transform.position + new Vector3(Mathf.Cos(zRotation - 45) * radius, Mathf.Sin(zRotation - 45) * radius, 0f);