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 SpaceSloth · Sep 30, 2013 at 03:25 PM · axislookatlockcompass

Look At while locked in a position

Hi there!

I've got a compass that's slightly rotated on the Z axis. (I'm using a game object for a GUI instead of a GUItexture, personal choice).

Now I'd love to get the needle of said compass (separated as it's own mesh) to always look at a certain object far away. I've tried the standard lookat, and obviously the needle rotates oddly on all the axis. I've also tried to lock it on one axis, but it doesn't only not work, I suspect it'll lock it flat on an axis, which won't do because my compass isn't flat.

Here's my beautiful non working code.

 using UnityEngine;
 using System.Collections;
 
 public class compass : MonoBehaviour {
     
     public GameObject target;
 
     // Use this for initialization
     void Start () {
         
     
     }
     
     // Update is called once per frame
     void Update () {
         
          transform.LookAt(Vector3(target.x, transform.position.y, target.z));
     }
 }

Any help in C# would be greatly appreciated, thank you!

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 robertbu · Sep 30, 2013 at 03:43 PM

I'm going to assume your compass is at some arbitrary angle aligned to the axes. If so, the solution is to project your target point onto a plane passing through the compass at the angle of the compass. So go out and get the Math3d script from the Unity Wiki, and add it to your project.

The method you want from the script is ProjetPointOnPlane(). Assuming your compass is constructed so that the face is transform.up, you code will be:

 void Update() {
    Vector3 v3 =  ProjectPointOnPlane(compass.transform.up, compass.transform.position, target.transform.position;
    transform.LooAt(v3);
 }

Actually you should be able to use the position and rotation of the needle as well if the angle of the compass does not change during the game:

 void Update() {
    Vector3 v3 =  ProjectPointOnPlane(transform.up,transform.position, target.transform.position;
    transform.LooAt(v3);
 }

P.S. The code you posted would have generated a syntax error since target is a game object and game object don't have x, y, and z, components. It should be 'target.transform.position.z'. Or you can make the code (original or recommended change) more efficient by making target a Transform.

Comment
Add comment · Show 1 · 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
avatar image SpaceSloth · Oct 01, 2013 at 12:01 PM 0
Share

Thank you! It actually works now.

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

14 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

Related Questions

I want to create a 3D compass (like in Bioshock) but it keeps rotating on all axes, including Y, even though it should only rotate on X and Z... (Code) 0 Answers

LookAt Limit Up and Down rotation only 2 Answers

Rotating an object around Z axis using Lerp 2 Answers

Creating a multiple part turret what locks onto certain axis. 4 Answers

How to lock the y axis in camera script? 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