• 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
1
Question by 124c41 · Oct 19, 2014 at 09:51 PM · transform.rotate

Make GameObject look at one target on x/z axis and another on y axis

Hello,

I've spend the last hours looking for a solution and tried different approaches without success. Now I'm very much at the end of the line and can't wrap my head around the problem anymore. A little push in the right direction would be appreciated.

What I'm trying to do: I'm working on a small project implementing a rts-type control scheme. The Player should be able to order Objects around, but not on a flat plane but on the surface of a sphere. Objects need not stick to the sphere, but move freely around it to get to their target position. This works fine so far.

Now, for cosmetic reasons, I want the objects to look toward the waypoint on two axis, but to the center of the sphere on the third, so that they are more or less upright relative to the sphere. In flight terms: The Object pitch and yaw should be in the direction of the waypoint, its roll in the direction of the spheres center (http://en.wikipedia.org/wiki/Flight_dynamics#mediaviewer/File:Rollpitchyawplain.png).

Here is the last working state of my script:

 void Update () {

     if (waypointsList.Count > 0) {

         //Find Vector to the Waypoint
         Vector3 VectorToWayPoint = waypointsList[0] - transform.position;

         Debug.DrawLine(new Vector3(0,0,0), waypointsList[0], Color.yellow);
         Debug.DrawLine(new Vector3(0,0,0), transform.position, Color.green);

         //Get intermediate Waypoint
         Vector3 VectorIntermediateWayPoint = (waypointsList[0] - transform.position)*.1f + transform.position;
         Debug.DrawLine(transform.position, VectorIntermediateWayPoint, Color.blue);

         //How far is the intermediate Waypoint along the route?
         float DistanceToRoute = Vector3.Distance(transform.position, VectorIntermediateWayPoint)/Vector3.Distance(transform.position, waypointsList[0]);

         //Get Distance from World Center at Intermediate WayPoint
         float DistanceFromCenter = Mathf.Lerp(Vector3.Distance(ZeroVector, transform.position), Vector3.Distance(ZeroVector, waypointsList[0]), DistanceToRoute);

         //Normalize the intermediate Waypoint and push it out by interpolated distance
         VectorIntermediateWayPoint = Vector3.Normalize(VectorIntermediateWayPoint)*DistanceFromCenter;

         Debug.DrawLine(new Vector3(0,0,0), VectorIntermediateWayPoint, Color.red); 


         //Move the ship
         transform.position = Vector3.MoveTowards(transform.position, transform.position + transform.forward, 7.0F*Time.deltaTime);

         //Turn the ship
         Vector3 targetLookDirection = VectorIntermediateWayPoint - transform.position;
         Vector3 newLookDirection = Vector3.RotateTowards(transform.forward, targetLookDirection, 1.0F*Time.deltaTime, .0F);

         transform.rotation = Quaternion.LookRotation(newLookDirection);

         if (Vector3.Distance(transform.position, waypointsList[0]) < .5F) {
             waypointsList.RemoveAt(0);
         }
     }
 }

Thank you very much!

Edit:

transform.LookAt(Waypoint, VectorAwayFromCenter) makes the object behave like I want, but there isn't an option to turn gradually over time with it, is there?

Oh boy...

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by 124c41 · Oct 20, 2014 at 03:15 AM

Ok, I figured it out somehow.

If anybody has the same problem, here is my solution:

             Vector3 targetDirection = VectorIntermediateWayPoint - transform.position;
             Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, turnRate*Time.deltaTime, .0f);
 
             transform.LookAt(transform.position + newDirection, transform.position - ZeroVector);

Where ZeroVector is the position of the spheres center, in this case (0,0,0).

Edit: If anybody has a more elegant and versatile solution, I'd be happy to hear about it.

Comment
Add comment · Share
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Turn page, smooth rotate 1 Answer

unity how to make objects look at random objects with the same tag 1 Answer

How to stop a child from orbiting around parent? 0 Answers

Issues with copying Y axisrotation of another object 2 Answers

Not Enough Rotation 3 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