This site uses strictly necessary cookies. More Information

X- Home /

# Rotating a parent so a child is facing a point.

I finally got something working. Don't know if its the most effective method or if anyone can make sense of it, but i made it and it works.

`static function turret_offset( Turret_Center:Transform, spwner_cntr:Transform) { //this is used when the turret first spawns to get the x-axis offset from the turret's yaw and the projectile spawner. var turret_offset_vec:float; turret_offset_vec = Vector3.Distance(Vector3(Turret_Center.transform.position.x,0,0), Vector3(spwner_cntr.transform.position.x,0,0)); return turret_offset_vec; }`

` `

static function Turret_Look( Turret_Yaw:Transform, BulletSpawner:Transform, spawner_x_offset:float) { //We need to pitch the turret up and down so that the projectile spawner lines up with the crosshair/target. We'll do this by getting the angles between //the direction the spawner is looking and what the spawner is suppose to be looking at, and then subtract/add the difference.

```
//If the turret projectile spawner is offset along the x-axis this will throw off our Vector3.Angle. We must shift to align with the spawner.
//the varable "spawner_x_offset" is the distance on the x-axis from the turret yaw and the spawner when the turret is created.
var spawner_center:Vector3;
spawner_center.x = (Turret_Yaw.transform.position.x + (spawner_x_offset * -Mathf.Cos((Turret_Yaw.transform.eulerAngles.y + 180)*Mathf.PI/180)));
spawner_center.z = (Turret_Yaw.transform.position.z + (spawner_x_offset * Mathf.Sin((Turret_Yaw.transform.eulerAngles.y + 180)*Mathf.PI/180)));
spawner_center.y = crosshair.transform.position.y;
//We now slign with the crosshair with the x-axis offset.
var spawner_to_target:Vector3;
spawner_to_target.x = (spawner_center.x + ((Main_Script.crosshair_distance) * -Mathf.Cos((Turret_Yaw.transform.eulerAngles.y + 90)*Mathf.PI/180)));
spawner_to_target.z = (spawner_center.z + ((Main_Script.crosshair_distance) * Mathf.Sin((Turret_Yaw.transform.eulerAngles.y + 90)*Mathf.PI/180)));
spawner_to_target.y = crosshair.transform.position.y;
//We get the angle between the spawner and the crosshair. the ray is used later.
var targetDir = spawner_to_target - BulletSpawner.transform.position;
var cross_ray = Ray(BulletSpawner.transform.position, targetDir);
var anglecross = Vector3.Angle(targetDir, BulletSpawner.transform.forward);
//We get the angle the projectile is looking by drawing a ray and getting a point on the ray's path.
var spawner_ray:Ray = Ray(BulletSpawner.transform.position, BulletSpawner.transform.forward);
targetDir = spawner_ray.GetPoint(1) - BulletSpawner.transform.position;
var anglelook = Vector3.Angle(targetDir, BulletSpawner.transform.forward);
//this is how much the turret will pitch
var Angle_Diff:float = anglecross - anglelook;
//The difference never goes negative so we have to check to see if we need to pitch up or down.
//This get a little weird since we flip the result positive or negative and then subtract that from the pitch in the turret script.
if(spawner_ray.GetPoint(1).y < cross_ray.GetPoint(1).y)
{
return Angle_Diff;
}
else if (spawner_ray.GetPoint(1).y > cross_ray.GetPoint(1).y)
{
return -Angle_Diff;
}
```

`} `

Whats in the rocket_turret script file.

`function Start () { Rocket_Launcher_Pitch = transform.Find("Rocket_Base/Rocket_Base_Support_01/Rocket_Base_Support_02"); Rocket_Launcher_Yaw = transform.Find("Rocket_Base"); Rocket_Launcher_Right_Spawn_Pos = transform.Find("Rocket_Base/Rocket_Base_Support_01/Rocket_Base_Support_02/Rocket_Base_Support_Right/Rocket_Base_Launcher_Right/Rocket_Spawn_Right"); rocket_offset = Main_Script.turret_offset(Rocket_Launcher_Yaw, Rocket_Launcher_Right_Spawn_Pos);`

` `

}

`function LateUpdate () { Rocket_Launcher_Yaw.transform.eulerAngles.y = Main_Script.game_camera.transform.eulerAngles.y; `

Rocket_Launcher_Pitch.transform.eulerAngles.x -= Main_Script.Turret_Look(Rocket_Launcher_Yaw, Rocket_Launcher_Right_Spawn_Pos, rocket_offset); }

I was having trouble rotating a parent so a child faced a point. I posted this problem and was updating this thread with what I got working and needed help with, till I solved my own problem.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.