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 Scharkee · Mar 30, 2011 at 03:51 AM · rotateparentchildfacing

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); }

Comment
Add comment · Show 2
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
avatar image Statement · Apr 02, 2011 at 11:03 AM 0
Share

Grats for getting something working. The question is?

avatar image Scharkee · Apr 03, 2011 at 10:13 AM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

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

No one has followed this question yet.

Related Questions

Make a simple tree 1 Answer

Rotate parent so child is facing a specific rotation 1 Answer

A way to rotate object 90 degrees when it becomes child of parent? 1 Answer

Align two parents based on child position and rotation 1 Answer

Simulate Child Parent Relationship (parent constraint) 2 Answers


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